Example of creating a brush on iOS (double stroke effect)

Source: Internet
Author: User

Define a UIView: The main is to add a uiimageview in this view, drawing all in this uiimageview inside

@property (nonatomic) cgpoint prepoint;  The point at which the finger precedes the move event @property (nonatomic) cgpoint oppsitepoint;  The point of the finger before entering 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 handle the finger events

#pragma mark-deal touch-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "Touchesbegan");    The following two sentences know the point of the finger on the screen of the information uitouch* touch = [touches anyobject];        Cgpoint point = [Touch locationinview:self];        if (touch) {self.prepoint = point;        _oppsitepoint = point;    _oppsitepoint.x = 320.0f-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 (Uigraphicsgetcurrentcontext (), 2.0f); Cgcontextsetrgbstrokecolor (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, 320.0f-point.x,               POINT.Y);        Cgcontextaddlinetopoint (Uigraphicsgetcurrentcontext (), Point.x, POINT.Y);        Cgcontextstrokepath (Uigraphicsgetcurrentcontext ());        _drawimage.image = Uigraphicsgetimagefromcurrentimagecontext ();                Uigraphicsendimagecontext ();        _prepoint = point;        _oppsitepoint = point;                    _oppsitepoint.x = 320.0f-point.x;        }}-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{NSLog (@ "touchesended"); The effect of this paragraph is if the screenClick to draw a point uigraphicsbeginimagecontext (self.frame.size);                                            [_drawimage.image drawinrect:cgrectmake (0, 0, _drawimage.frame.size.width, _drawimage.frame    . Size.Height)];    Cgcontextsetlinecap (Uigraphicsgetcurrentcontext (), kcglinecapround);    Cgcontextsetlinewidth (Uigraphicsgetcurrentcontext (), 3.0f);    Cgcontextsetrgbstrokecolor (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.