Master Stroke Master Stroke -4 years ago 89
iOS Question

Draw line between two points and drag/curve that line using finger in iOS?

I'm trying to add line between two points based on user touch(start point will be user's first touch & end point will be user's second touch on the view).I was able to draw it using below code.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)
NSLog(@"touch count 2");
if(isOdd)//View already has user's start point
nextPoint = [touch locationInView:self.view];
point2 = nextPoint;
//nextPoint.y -= 20;
[self drawLineFromPoint:currentPoint toPoint:nextPoint];
currentPoint = nextPoint;
isOdd = YES;
currentPoint = [touch locationInView:self.view];
point1 = currentPoint;
isOdd = YES;
-(void)drawLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:start];
[path addLineToPoint:end];
CAShapeLayer *shapeLayer = [CAShapeLayer new];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [UIColor greenColor].CGColor;
shapeLayer.lineWidth = 3.0;
[self.myView.layer addSublayer:shapeLayer];

Now my question is, Is it possible to curve that line when user tries to drag/move that line using finger?

Answer Source

If your question was to curve the line which was already drawn, then you can try the following:

As you would know the starting and ending points of the line, you can redraw a curve as the finger moves using

- addQuadCurveToPoint:controlPoint:

function of UIBezierPath, where the control point would be the current touch point during the move. You'll have to move to the start point before calling this function using

- moveToPoint:

and make the end point as the destination point for the curve function mentioned above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download