How can I erase the line between two CGpoints?

enter image description here Hi guys, I am breaking a line in ipad when the user touches the screen and drags a finger. The problem is creating a line at each point (touchMoved :) we drag it. but in consequence of this there should be only one not much. How can I delete or delete the last row after creating a new one. Here is my code

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if(ArrowDraw==YES){ NSLog(@"ArrowDrawing"); if ([[event allTouches]count]==1){ UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:FullImageView]; UIGraphicsBeginImageContext(self.view.frame.size); [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; CGContextMoveToPoint(UIGraphicsGetCurrentContext(), firstPoint.x, firstPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1 ); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); CGContextStrokePath(UIGraphicsGetCurrentContext()); self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); [self.tempDrawImage setAlpha:opacity]; UIGraphicsEndImageContext(); //firstPoint = currentPoint; NSLog(@"TouchMoving x=%fy=%f",firstPoint.x,firstPoint.y); } UIGraphicsBeginImageContext(self.FullImageView.frame.size); [self.FullImageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; self.FullImageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.tempDrawImage.hidden=YES; } } 
+4
source share
1 answer

The key is not to update the image, but simply draw an arrow on top of the image. Then replace the arrow with another when touchesMoved appears.

For example, I can use QuartzCore.framework by adding it to your target β€œLink Binary With Libraries” and adding the following line to the top of your .m:

 #import <QuartzCore/QuartzCore.h> 

Then you can define a new ivar for your CAShapeLayer :

 CAShapeLayer *shapeLayer; 

Finally, upgrade your touchesMoved to the following value:

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (ArrowDraw==YES){ if ([[event allTouches]count]==1) { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:FullImageView]; if (!shapeLayer) { shapeLayer = [CAShapeLayer layer]; shapeLayer.lineWidth = 1; shapeLayer.strokeColor = [[UIColor blackColor] CGColor]; shapeLayer.fillColor = [[UIColor clearColor] CGColor]; [FullImageView.layer addSublayer:shapeLayer]; } UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:firstPoint]; [path addLineToPoint:currentPoint]; shapeLayer.path = [path CGPath]; } } } 

You can modify this UIBezierPath to include an arrow if you need it, for example:

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (ArrowDraw==YES){ if ([[event allTouches]count]==1) { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:FullImageView]; if (!shapeLayer) { [self createShapeLayer]; } shapeLayer.path = [[self arrowPathFrom:firstPoint to:currentPoint arrowheadSize:10.0] CGPath]; } } } - (void)createShapeLayer { shapeLayer = [CAShapeLayer layer]; shapeLayer.lineWidth = 1; shapeLayer.strokeColor = [[UIColor blackColor] CGColor]; shapeLayer.fillColor = [[UIColor clearColor] CGColor]; [FullImageView.layer addSublayer:shapeLayer]; } - (UIBezierPath *)arrowPathFrom:(CGPoint)start to:(CGPoint)end arrowheadSize:(CGFloat)arrowheadSize { UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:start]; [path addLineToPoint:end]; // add arrowhead CGFloat angle = atan2(end.y - start.y, end.x - start.x) + M_PI * 3.0 / 4.0; [path addLineToPoint:CGPointMake(cos(angle) * arrowheadSize + end.x, sin(angle) * arrowheadSize + end.y)]; [path addLineToPoint:end]; angle = atan2(end.y - start.y, end.x - start.x) - M_PI * 3.0 / 4.0; [path addLineToPoint:CGPointMake(cos(angle) * arrowheadSize + end.x, sin(angle) * arrowheadSize + end.y)]; [path addLineToPoint:end]; return path; } 
+2
source

Source: https://habr.com/ru/post/1501452/


All Articles