A
For more details about UIBezierPath Click Here.UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. We can set the geometry and attributes separately and can change them independent of one another. Once we have the object configured the way you want it, you can tell it to draw itself in the current context. Because the creation, configuration, and rendering process are all distinct steps, Bezier path objects can be reused easily in code. We can even use the same object to render the same shape multiple times, perhaps changing the rendering options between successive drawing calls. Now, we are going to create a simple drawing app using UIBezierpath.
First, create a subclass of UIView Class.
Suppose name is SketchView
Inside SketchView.h
@interface SketchView : UIView
{
UIBezierPath *scriblePath;
}
Add following code in SketchView.m
@implementation SketchView
-(instancetype) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
scriblePath = [[UIBezierPath alloc] init];
scriblePath.lineCapStyle=kCGLineCapRound;
scriblePath.miterLimit=0;
scriblePath.lineWidth = 2; //Set Stroke with
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
[[UIColor blackColor] setStroke];
[scriblePath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
#pragma mark -
-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
[scriblePath moveToPoint:touchPoint];
}
-(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:self.superview];
[scriblePath addLineToPoint:touchPoint];
[self setNeedsDisplay];
}
@end

