2013-02-07 39 views
0

所以我仍然在学习这个Objective C百灵,我已经打了一个残端。所有我想要做的是使用从Sliderchanged值来填充“CGFloat的的lineWidth =”,希望有改变crumbPath线的宽度的最终结果...使用UISlider的值更改另一个变量?

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

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 ...

回答

0

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

例子:

-(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 
} 
+0

我不太确定mySliderControl.value的来源,它是怎么来的它是一个IBOutlet?我应该这样做而不是将滑块添加为动作?无知的道歉。感谢您的帮助,非常感谢。 –

+0

您的滑块是否保存到某个变量的某个位置?您是否将它连接到Interface Builder中的IBOutlet? – Jeremy

+0

好吧,所以我连接滑块作为出口@属性(弱,非原子)IBOutlet UISlider * sliderChanged;在界面生成器中,即时获取没有错误,输出显示滑动值随着我移动它(优秀)而改变。 2013-02-07 16:16:03.963地图[1836:c07]滑块值= 60.199234 2013-02-07 16:16:03.980地图[1836:c07]滑块值= 60.444443 2013-02-07 16 :16:03.997地图[1836:c07]滑块值= 60.689655 但crumbPath不显示在模拟器中? –

-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); 

} 
+0

我得到一个错误,在这一行这个'CrumbPath * crumbs =(CrumbPath *) (self.overlay);”说''float'类型的操作数不能转换为指针类型。' –

+0

我将你的代码复制到了一个不同的函数中。 –

+0

如果你不是在这种方法得到错误 - (空)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext的:(CGContextRef)方面 那么您只需建立UISlider的IBOutlet中,并使用该实例中以上功能e。g CGFloat lineWidth = myUISlider.value –