Draw an line in iOS objective C using hand Drawing
Solution:
If you want to draw an line using your hand then use the below code.
Using the touches start and end method to draw the line.
If you want to draw an line using your hand then use the below code.
Using the touches start and end method to draw the line.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
UIBezierPath *myPath;
CGPoint firstPoint; // (3)
CGPoint initPoint;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
myPath = [UIBezierPath bezierPath];
firstPoint = touchLocation;
initPoint = touchLocation;
[myPath moveToPoint:touchLocation];
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UIBezierPath *path = [UIBezierPath bezierPath];
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
[path moveToPoint:CGPointMake(firstP oint.x , firstPoint.y)];
[path addLineToPoint:CGPointMake( touchLocation.x, touchLocation.y)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor blackColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
self.view.layer.sublayers = nil;
[self.view.layer addSublayer:shapeLayer];
firstPoint = touchLocation;
}
- (void)touchesEnded:(NSSet<UITo uch *> *)touches withEvent:(UIEvent *)event {
UIBezierPath *path = [UIBezierPath bezierPath];
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
[path moveToPoint:CGPointMake(initPo int.x , initPoint.y)];
[path addLineToPoint:CGPointMake( touchLocation.x, touchLocation.y)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor blackColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[self.view.layer addSublayer:shapeLayer];
firstPoint = touchLocation;
}
@end
Comments
Post a Comment