0

だから私はまだこの Objective C のひばりを学んでいて、切り株にぶつかりました。私がやりたいのは、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 に答える