Example of creating a paint brush in ios (double strokes)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.