0
我可以繪製路徑並通過手勢識別器使視圖可拖拽。我將如何在路徑上拖拽一點。我想我需要確定這一點來檢查它在視圖中的位置,並將它的位置重置到拖動的位置,但這是我不知道該怎麼做的。如何使路徑中的特定點能夠通過手勢拖拽。 iPhone
我可以繪製路徑並通過手勢識別器使視圖可拖拽。我將如何在路徑上拖拽一點。我想我需要確定這一點來檢查它在視圖中的位置,並將它的位置重置到拖動的位置,但這是我不知道該怎麼做的。如何使路徑中的特定點能夠通過手勢拖拽。 iPhone
Bro有檢測CGPath
或UIBezierPath
上的命中的功能。 ON touchesBegan
:您可以使用以下任一方法來檢測命中點是否在路徑上。
對於UIBezierPath
:- (BOOL)containsPoint:(CGPoint)point
爲GGPath
:
bool CGContextPathContainsPoint (
CGContextRef context,
CGPoint point,
CGPathDrawingMode mode
);
那麼,如果這點是你的路徑上,那麼你可以設置一個標誌。並在touchesEnd
方法你可以得到翻譯點。但是需要重新繪製路徑。路徑不會彈性。
編輯:有一兩件事,我對你的情況的工作是CAShapeLayer.
CAShapeLayer可以得出一個UIBazierPath
或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