我试图在iOS上绘制和编辑多条线。每行的每一端都有一个UIView充当句柄,因此,一旦绘制了该行,用户就可以拖动每端并重新绘制该行。
我目前正在使用UIBezierPath在视图上绘制CAShapelayer。我遇到的问题是最好的方式来绘制另一条线,并且用户点击了哪条线都可以对其进行编辑。
有人对最佳方法有任何想法吗? CAShapelayer是最好的选择吗?
视频链接可能会更好地显示我要实现的目标。
https://www.dropbox.com/s/8rpt2azrs3uk6vr/Line%20Example.mov?dl=0
我到目前为止已完成的代码示例如下:
//画一条线 在这里,我从两个接触点创建路径,并在视图上绘制CAShapeLayer。我还创建了一个自定义对象“线”来存储路径和shapelayer。
-(void)DrawLineFrom:(CGPoint)pointA to:(CGPoint)pointB
{
NSLog(@"Drawing line X:%f Y:%f - X:%f Y:%f", pointA.x, pointA.y, pointB.x, pointB.y);
UIBezierPath* path = [[UIBezierPath alloc]init];
[path moveToPoint:pointA];
[path addLineToPoint:pointB];
[path addLineToPoint:CGPointMake(pointB.x, pointB.y+2)];
[path addLineToPoint:CGPointMake(pointA.x, pointA.y+2)];
[path addLineToPoint:pointA];
[path closePath];
currentLine.bPath = path;
if (!shapeLayer)
{
shapeLayer = [LineLayer layer];
[shapeLayer setFrame:self.view.frame];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [UIColor redColor].CGColor; //etc...
shapeLayer.lineWidth = 2.0; //etc...
shapeLayer.parent = currentLine;
currentLine.shapeLayer = shapeLayer;
[self.view.layer addSublayer:shapeLayer];
}
else
{
shapeLayer.path = path.CGPath;
}
[self ExitDrawMode];
}
//创建一个句柄 这将在两端创建两个视图,并添加长按手势。
-(HandleView *)MakeLineHandleForPoint:(int)point atLocation:(CGPoint)loc
{
HandleView *pointView = [[HandleView alloc]initWithFrame:CGRectMake(loc.x-10, loc.y-10, 20, 20)];
pointView.layer.cornerRadius = 10;
pointView.backgroundColor = [UIColor redColor];
UILongPressGestureRecognizer *LP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLp:)];
[pointView addGestureRecognizer:LP];
LP.delegate = self;
LP.minimumPressDuration = 0.0;
pointView.userInteractionEnabled = YES;
pointView.tag = point;
pointView.lineParent = currentLine;
return pointView;
}
最后,在此处处理手势。这可以正常工作,但是将始终移动最后绘制的线条。即使我选择了第一个。
-(void)handleLp:(UILongPressGestureRecognizer *)发送者 { CGPoint loc = [发件人locationInView:self.view];
[sender view].center = loc;
HandleView *handleView = [sender view];
if ([sender view].tag == 0) {
currentLine.pointA = loc;
[self DrawLineFrom:loc to:currentLine.pointB];
}
if ([sender view].tag == 1) {
currentLine.pointB = loc;
[self DrawLineFrom:currentLine.pointA to:loc];
}
}
非常感谢任何帮助。提前致谢。