0

所以我还在学习这个Objective C lark,我遇到了困难。我想要做的就是使用 Sliderchanged 中的值来填充'CGFloat lineWidth =',希望最终结果是改变 crumbPath 线的宽度......

** * ** * ** * ** * ** * ***编辑* ** * ** * ** * ** * ** * ** * **

CrumbPathView.h

#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
{
NSLog(@"slider value = %f", sender.value);
 }

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



CGFloat lineWidth = 30;



// 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
{
}

- (IBAction)sliderChanged:(id)sender;

@end

我已经从 .h & .m 添加了其余的代码......

4

2 回答 2

0

我想你大部分都在那里。您的滑块可能存储在一个属性中......当您需要它的值时,只需像检查任何其他属性一样检查它。

例子:

-(IBAction)sliderChanged:(UISlider *)sender
{
    //[self drawMapRect...]
}

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:CGContextRef)context
{
     CrumbPath *crumbs = (CrumbPath *)(self.overlay);
     CGFloat lineWidth = self.mySliderControl.value;
     //Do something with lineWidth
}
于 2013-02-07T15:07:49.877 回答
-1

你试过这个吗?

-(IBAction)sliderChanged:(UISlider *)sender
{
NSLog(@"slider value = %f", sender.value);
CrumbPath *crumbs = (CrumbPath *)(self.overlay);
 CGFloat lineWidth = (UISlider*)sender.value;
//Set value on crumbpath using slider value

 }

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

}
于 2013-02-07T15:08:09.220 回答