Dynamically changing lineWidth using UISlider?

2019-07-31 15:01发布

问题:

I have imported the CrumbPath.h & .m and CrumPathView.h & .m into my project from the Sample code provided by Apple.

It is working fine, until I want to control the lineWidth of the CrumbPath, In a previous question we solved how to use value from 'Sliderchanged' to populate the 'CGFloat lineWidth ='

Linked here: Use value from UISlider to change another variable?

Now I have hit the problem that the line is not showing at all in the simulator...

CrumbPathView.m

#import "CrumbPathView.h"

#import "CrumbPath.h"

#import "FirstViewController.h"


@interface CrumbPathView (FileInternal)
- (CGPathRef)newPathForPoints:(MKMapPoint *)points
                  pointCount:(NSUInteger)pointCount
                    clipRect:(MKMapRect)mapRect
                   zoomScale:(MKZoomScale)zoomScale;


@end

@implementation CrumbPathView



-(IBAction)sliderChanged:(UISlider *)sender
{


}

- (void)drawMapRect:(MKMapRect)mapRect
      zoomScale:(MKZoomScale)zoomScale
      inContext:(CGContextRef)context
{
CrumbPath *crumbs = (CrumbPath *)(self.overlay);
CGFloat lineWidth = self.sliderChanged.value;







// outset the map rect by the line width so that points just outside
// of the currently drawn rect are included in the generated path.
MKMapRect clipRect = MKMapRectInset(mapRect, -lineWidth, -lineWidth);

[crumbs lockForReading];
CGPathRef path = [self newPathForPoints:crumbs.points
                                pointCount:crumbs.pointCount
                                  clipRect:clipRect
                                 zoomScale:zoomScale];
[crumbs unlockForReading];

if (path != nil)
{
    CGContextAddPath(context, path);
    CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 1.0f, 0.5f);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, lineWidth);
    CGContextStrokePath(context);
    CGPathRelease(path);
}
}




@end

@implementation CrumbPathView (FileInternal)




static BOOL lineIntersectsRect(MKMapPoint p0, MKMapPoint p1, MKMapRect r)
{
double minX = MIN(p0.x, p1.x);
double minY = MIN(p0.y, p1.y);
double maxX = MAX(p0.x, p1.x);
double maxY = MAX(p0.y, p1.y);

MKMapRect r2 = MKMapRectMake(minX, minY, maxX - minX, maxY - minY);
return MKMapRectIntersectsRect(r, r2);
}

#define MIN_POINT_DELTA 5.0

- (CGPathRef)newPathForPoints:(MKMapPoint *)points
                  pointCount:(NSUInteger)pointCount
                    clipRect:(MKMapRect)mapRect
                   zoomScale:(MKZoomScale)zoomScale
{
// The fastest way to draw a path in an MKOverlayView is to simplify the
// geometry for the screen by eliding points that are too close together
// and to omit any line segments that do not intersect the clipping rect.  
// While it is possible to just add all the points and let CoreGraphics 
// handle clipping and flatness, it is much faster to do it yourself:
//
if (pointCount < 2)
    return NULL;

CGMutablePathRef path = NULL;

BOOL needsMove = YES;

#define POW2(a) ((a) * (a))

// Calculate the minimum distance between any two points by figuring out
// how many map points correspond to MIN_POINT_DELTA of screen points
// at the current zoomScale.
double minPointDelta = MIN_POINT_DELTA / zoomScale;
double c2 = POW2(minPointDelta);

MKMapPoint point, lastPoint = points[0];
NSUInteger i;
for (i = 1; i < pointCount - 1; i++)
{
    point = points[i];
    double a2b2 = POW2(point.x - lastPoint.x) + POW2(point.y - lastPoint.y);
    if (a2b2 >= c2) {
        if (lineIntersectsRect(point, lastPoint, mapRect))
        {
            if (!path) 
                path = CGPathCreateMutable();
            if (needsMove)
            {
                CGPoint lastCGPoint = [self pointForMapPoint:lastPoint];
                CGPathMoveToPoint(path, NULL, lastCGPoint.x, lastCGPoint.y);
            }
            CGPoint cgPoint = [self pointForMapPoint:point];
            CGPathAddLineToPoint(path, NULL, cgPoint.x, cgPoint.y);
        }
        else
        {
            // discontinuity, lift the pen
            needsMove = YES;
        }
        lastPoint = point;
    }
}

#undef POW2

// If the last line segment intersects the mapRect at all, add it unconditionally
point = points[pointCount - 1];
if (lineIntersectsRect(lastPoint, point, mapRect))
{
    if (!path)
        path = CGPathCreateMutable();
    if (needsMove)
    {
        CGPoint lastCGPoint = [self pointForMapPoint:lastPoint];
        CGPathMoveToPoint(path, NULL, lastCGPoint.x, lastCGPoint.y);
    }
    CGPoint cgPoint = [self pointForMapPoint:point];
    CGPathAddLineToPoint(path, NULL, cgPoint.x, cgPoint.y);
}

return path;
}

@end

CrumbPathView.h

#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>

@interface CrumbPathView : MKOverlayView
{

}

@property (weak, nonatomic) IBOutlet UISlider *sliderChanged;


@end

When moving the slider the Output console logs the changes, but nothing changes visually. No Errors, but as I mentioned the CrumbPath is not showing at all... as if the lineWidth was set to '0'.