0

我可以通过手势识别器绘制路径并使视图可拖动。我如何在可拖动的路径上指出一点。我想我需要确定该点以检查它在视图中的位置并将其位置重置为它被拖动到的点,但这就是我不知道该怎么做。

4

1 回答 1

1

兄弟有检测命中的功能CGPathor UIBezierPath。ON touchesBegan:方法您可以使用以下方法之一来检测命中点是否在路径上。

对于UIBezierPath- (BOOL)containsPoint:(CGPoint)point

对于GGPath

 bool CGContextPathContainsPoint (
   CGContextRef context,
   CGPoint point,
   CGPathDrawingMode mode
);

然后,如果这一点在您的路径上,那么您可以设置一个标志。而在touchesEnd方法中,您可以获得翻译点。但是需要重新绘制路径。路径不会是弹性的。

编辑:我为您的案例所做的一件事是CAShapeLayer.CAShapeLayer 可以绘制一个UIBazierPathor CGPathRef。而且它还通过它的 strokeend 和 strokestart 属性进行动画处理。请参阅以下代码以了解绘制路径的想法CAShapeLayer

    UIBezierPath *path = [UIBezierPath bezierPath];
    // Draw your path acording to your requirements
    // Remember that you don't need to stroke path in this implementation
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath; // path is a UIBezierPath object
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineWidth = 1.5;
    shapeLayer.strokeStart = 0.0;
    shapeLayer.strokeEnd = 1.0;
    [shapeLayer renderInContext:ctx]; // you can also use addSubLayer: and drawInContext
于 2012-12-05T14:37:41.767 回答