Define a UIView: Add a UIImageView in the View, and draw images in the UIImageView.
@ Property (nonatomic) CGPoint prePoint; // point of the finger before the move event @ property (nonatomic) CGPoint oppsitePoint; // The point of the finger before the move event @ property (nonatomic, retain) UIImageView * drawImage;-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// Initialization code self. drawImage = [[UIImageView alloc] initWithImage: nil]; self. drawImage. frame = self. frame; [self addSubview: _ drawImage];} return self ;}
Then process finger events
# Pragma mark-deal touch-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "touchesBegan "); // The following two sentences show the point information of the finger on the screen. UITouch * touch = [touches anyObject]; CGPoint point = [touch locationInView: self]; if (touch) {self. prePoint = point; _ oppsitePoint. x = 320366f-point. x ;}}- (void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "touchesMoved"); UITouch * touch = [touches anyObject]; if (touch) {CGPoint point = [touch locationInView: self]; UIGraphicsBeginImageContext (self. frame. size); [_ drawImage. image drawInRect: CGRectMake (0, 0, _ drawImage. frame. size. width, _ drawImage. frame. size. height)]; CGContextSetLineCap (UIGraphicsGetCurrentContext (), kCGLineCapRound); CGContextSetLineWidth (rows (), 2.0f); sums (UIGraphicsGetCurrentContext (), 0.314, 0.486, 0.859, 1.0 ); CGContextBeginPath (UIGraphicsGetCurrentContext (); CGContextMoveToPoint (UIGraphicsGetCurrentContext (), _ prePoint. x, _ prePoint. y); CGContextAddQuadCurveToPoint (UIGraphicsGetCurrentContext (), _ prePoint. x, _ prePoint. y, point. x, point. y); CGContextMoveToPoint (UIGraphicsGetCurrentContext (), _ oppsitePoint. x, _ oppsitePoint. y); CGContextAddQuadCurveToPoint (UIGraphicsGetCurrentContext (), _ oppsitePoint. x, _ oppsitePoint. y, 320366f-point. x, point. y); // CGContextAddLineToPoint (UIGraphicsGetCurrentContext (), point. x, point. y); CGContextStrokePath (UIGraphicsGetCurrentContext (); _ drawImage. image = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); _ prePoint = point; _ oppsitePoint. x = 320366f-point. x ;}}- (void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "touchesEnded "); // This section is used to draw UIGraphicsBeginImageContext (self. frame. size); [_ drawImage. image drawInRect: CGRectMake (0, 0, _ drawImage. frame. size. width, _ drawImage. frame. size. height)]; CGContextSetLineCap (UIGraphicsGetCurrentContext (), kCGLineCapRound); CGContextSetLineWidth (rows (), 3.0f); sums (UIGraphicsGetCurrentContext (), 0.314, 0.486, 0.859, 1.0 ); CGContextBeginPath (UIGraphicsGetCurrentContext (); CGContextMoveToPoint (UIGraphicsGetCurrentContext (), _ prePoint. x, _ prePoint. y); CGContextAddLineToPoint (UIGraphicsGetCurrentContext (), _ prePoint. x, _ prePoint. y); CGContextStrokePath (UIGraphicsGetCurrentContext (); _ drawImage. image = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); self. isTouch = NO ;}
Below is