I draw a form layer using the UIBezier Path on a UIView:
CAShapeLayer *pathLayer = [CAShapeLayer layer]; pathLayer.frame=CGRectMake(-view.frame.origin.x, -view.frame.origin.y, view.frame.size.width, view.frame.size.height); pathLayer.path = path.CGPath; pathLayer.strokeColor = [[UIColor blackColor] CGColor]; pathLayer.fillColor = [[UIColor clearColor] CGColor];
On touch points, I want to draw this UIView, for this I do:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch=[touches anyObject]; CGPoint touchBeganPoint; touchBeganPoint=[touch locationInView:self]; actionString=@ "drawPencil"; previousPoint1 = [touch locationInView:self]; previousPoint2 = [touch locationInView:self]; currentPoint = [touch locationInView:self]; [self touchesMoved:touches withEvent:event]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; previousPoint2 = previousPoint1; previousPoint1 = currentPoint; currentPoint = [touch locationInView:self]; [self calculateMinImageArea:previousPoint1 :previousPoint2 :currentPoint]; } CGPoint midPoint(CGPoint p1, CGPoint p2)
Now, when I draw self.layer, it still produces the added sublevel (form layer). I want to draw only inside this path, I canβt go with [path containspoint ..], as in draw rect, the image I draw has some width and crosses the path or sometimes breaks its course.
One way to achieve this is to clip the UIView with its sublayer (CAShapelayer) and thus draw it again on uiview (this will clear the background separately from the form layer area). But cannot find how to pin it using CAShapeLayer.
Any suggestions or insights on how to achieve this smoothly can be of great help. Thanks!
ios objective-c core-graphics uiview uibezierpath
user3978087
source share