简体   繁体   中英

Draw straight line and create curve line dragging line

I would like to create an instrument for my app that draws straight line first and on the second touch and move user can create an adjustable curve line. So the idea is to set middle point of curve line when user touches and moves second time.

And i have no idea how to check second touch and work with previous path.

This method i use to draw simple line

Thank you!

- (void)setInitialPoint:(CGPoint)firstPoint
{
self.firstPoint  = firstPoint;
//[self moveToPoint:firstPoint]; //add yourStartPoint here
///[self addLineToPoint:endPoint];
}

- (void)moveFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint
{
self.lastPoint = endPoint;

   //  [self addLineToPoint:self.lastPoint];// add yourEndPoint here

}

- (void)draw { 
   UIBezierPath *path = [UIBezierPath bezierPath];
//draw a line

[path  moveToPoint:self.firstPoint]; //add yourStartPoint here
[path addLineToPoint:self.lastPoint];// add yourEndPoint here

[self.lineColor setStroke];

[path setLineWidth:3.0];

 [path stroke];
}

Second touch - hold a variable to track your state.

Curve - don't try to change a path, just use the start and end of the first line and create a UIBezierPath to draw as the user moves the touch point. Move the bezier to the start point, then use addCurveToPoint:controlPoint1:controlPoint2: using the end point and the current touch location.

To make it really fun, allow the user to touch with 2 fingers while drawing the second line and then you have values for both of the control points.

如果您只需要摆弄直线的中点,那么学习二次曲线对于您来说将很简单,因为它将只有一个控制点而不是两个控制点。首先仔细观察曲线的性质并根据您的选择(二次或贝塞尔曲线)调整控制点1,cp2(如果有),以下链接的起点和终点将以非常简单的方式帮助您了解贝塞尔曲线的性质链接

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM