iOS developing Bezier curves Uibezierpath (2)

Source: Internet
Author: User

Using Cashapelayer and Uibezierpath, you can draw some of the desired graphics in the DrawRect method that is not in view.

1:uibezierpath:uibezierpath is a class in UIKit that inherits from NSObject and can create vector-based paths. This class is a OC package for the core graphics framework about path. Use this class to define common shapes such as circles, polygons, and so on. We use line, ARC (ARC) to create complex curve shapes. The end of each line segment or curve segment is where the next begins. Each connected line or set of curved segments becomes subpath. A Uibezierpath object defines a complete path that includes one or more subpaths.

2:cashapelayer:cashapelayer, as the name implies, inherits from Calayer. Each Cashapelayer object represents an arbitrary shape (shape) that will be rendered to the screen. The specific shape is specified by its path (type Cgpathref) property. The normal calayer is a rectangle, so the frame property is required. Cashapelayer also needs to specify the frame value when initializing, but it does not have a shape, its shape is derived from its property path. Cashapelayer has a property different from the Calayer, and the property it inherits from Calayer does not work when it is drawn.

Example 1: Draw a circle

-(void) viewdidload {    [super viewdidload];         Create a cashapelayer    self.shapelayer = [Cashapelayer layer];    Self.shapeLayer.frame = CGRectMake (0, 0, 200, 200);//Set the size and position of the shapelayer    self.shapeLayer.position = Self.view.center;    Self.shapeLayer.fillColor = [Uicolor Clearcolor]. cgcolor;//fill Color is clearcolor         //Set the line width and color    self.shapeLayer.lineWidth = 1.0f;    Self.shapeLayer.strokeColor = [Uicolor Redcolor]. Cgcolor;         Create a circular Bezier curve    uibezierpath *circlepath = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, $)];         Let Bezier curve and Cashapelayer produce contact    self.shapeLayer.path = Circlepath.cgpath;         Add and display    [Self.view.layer addSublayer:self.shapeLayer];}

Now we're going to use the two parameters of Cashapelayer, Strokeend and Strokestart.

Stroke: Using the meaning of a stroke

Here's where the starting and ending pens are.

A stroke of 1 is a full circle, 0.5 is a half circle, and 0.25 is 1/4 laps. And so on

If we set the starting point to 0, the end point is set to 0.75.

Set the stroke start point

Self.shapeLayer.strokeStart = 0;

Self.shapeLayer.strokeEnd = 0.75;

Example 2: Draw two circles with one circle representing progress

Draw two round-(void) Createbezierpath: (cgrect) mybound{//Outer Circle _trackpath = [Uibezierpath bezierpathwitharccenter:self.view.c        Enter radius: (mybound.size.width-0.7)/2 startangle:0 ENDANGLE:M_PI * 2 clockwise:yes];;    _tracklayer = [Cashapelayer new];    [Self.view.layer Addsublayer:_tracklayer];    _tracklayer.fillcolor = nil; _tracklayer.strokecolor=[uicolor Graycolor].    Cgcolor;    _tracklayer.path = _trackpath.cgpath;    _tracklayer.linewidth=5;        _tracklayer.frame = Mybound; Inner circle _progresspath = [Uibezierpath bezierPathWithArcCenter:self.view.center radius: (mybound.size.width-0.7)/2 Starta        ngle:-m_pi_2 endangle: (M_PI * 2) * 0.7-m_pi_2 Clockwise:yes];    _progresslayer = [Cashapelayer new];    [Self.view.layer Addsublayer:_progresslayer];    _progresslayer.fillcolor = nil; _progresslayer.strokecolor=[uicolor Redcolor].    Cgcolor;    _progresslayer.linecap = Kcalinecapround;    _progresslayer.path = _progresspath.cgpath;    _progresslayer.linewidth=5; _progrEsslayer.frame = Mybound;} 

Example 3: Creating a rotating circle

-(void) viewdidload {[Super viewdidload];        Self.view.backgroundcolor=[uicolor Whitecolor];    [Self circlebezierpath]; Use the timer to simulate the case of a numeric input _timer = [Nstimer scheduledtimerwithtimeinterval:0.1 targe                                            T:self selector: @selector (Circleanimationtypeone) Userinfo:nil Repeats:yes];}    -(void) circlebezierpath{//create Cashapelayer Self.shapelayer = [Cashapelayer layer];    Self.shapeLayer.frame = CGRectMake (0, 0, 150, 150);    Self.shapeLayer.position = Self.view.center; Self.shapeLayer.fillColor = [Uicolor Clearcolor].        Cgcolor;    Set the width and color of the line self.shapeLayer.lineWidth = 2.0f; Self.shapeLayer.strokeColor = [Uicolor Redcolor].        Cgcolor;    Set Stroke start point self.shapeLayer.strokeStart = 0;        self.shapeLayer.strokeEnd = 0; Create a circular Bezier curve uibezierpath *circlepath = [Uibezierpath bezierpatHwithovalinrect:cgrectmake (0, 0, 150, 150)];        Let Bezier curve and cashapelayer produce contact self.shapeLayer.path = Circlepath.cgpath; Add and display [Self.view.layer AddSublayer:self.shapeLayer];}        -(void) circleanimationtypeone{if (self.shapeLayer.strokeEnd > 1 && self.shapeLayer.strokeStart < 1) {    Self.shapeLayer.strokeStart + = 0.1;    }else if (Self.shapeLayer.strokeStart = = 0) {self.shapeLayer.strokeEnd + = 0.1;    } if (self.shapeLayer.strokeEnd = = 0) {self.shapeLayer.strokeStart = 0;    } if (Self.shapeLayer.strokeStart = = self.shapeLayer.strokeEnd) {self.shapeLayer.strokeEnd = 0;    }}-(void) circleanimationtypetwo{cgfloat valueone = arc4random ()% 100/100.0f;        CGFloat valuetwo = arc4random ()% 100/100.0f; Self.shapeLayer.strokeStart = Valueone < valuetwo?    Valueone:valuetwo; Self.shapeLayer.strokeEnd = valuetwo > Valueone? Valuetwo:valueone;}

Example 4: Forming a five edge from a point drawing line

Draw a Pentagon-(void) fiveanimation{    uibezierpath *apath = [Uibezierpath Bezierpath];    Start point from top left to bottom right point    [Apath movetopoint:cgpointmake (100,100)];    Dash Point    [Apath addlinetopoint:cgpointmake (+)];    [Apath Addlinetopoint:cgpointmake ()];    [Apath Addlinetopoint:cgpointmake ()];    [Apath Addlinetopoint:cgpointmake ()];    [Apath Closepath];    Set point is a 5*5 small circle (self-added)    Uibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (100-5/2.0, 0, 5, 5) ];    [Apath Appendpath:path];        Cashapelayer *shapelayer = [Cashapelayer layer];    Set border color    shapelayer.strokecolor = [[Uicolor redcolor]cgcolor];    Set the fill color if only the side can be set to [Uicolor Clearcolor]    shapelayer.fillcolor = [[Uicolor bluecolor]cgcolor];    It is this sentence that is associated with each other (Uibezierpath and Cashapelayer):    shapelayer.path = Apath.cgpath;    [Self.view.layer Addsublayer:shapelayer];}

Example 5: Draw a dashed line

//Draw a dashed line-(void) createdottedline{cashapelayer *shapelayer = [Cashapelayer layer];    [Shapelayer SetBounds:self.view.bounds];    [Shapelayer SetPosition:self.view.center];        [Shapelayer Setfillcolor:[[uicolor Clearcolor] cgcolor]; Set Dash color to Blackcolor [shapelayer setstrokecolor:[[uicolor colorwithred:223/255.0 green:223/255.0 blue:223/255.0 Alpha        : 1.0f] Cgcolor];    3.0f set the width of the dashed line [Shapelayer setlinewidth:1.0f];        [Shapelayer Setlinejoin:kcalinejoinround]; Width of the 3= line 1 = spacing per line [Shapelayer setlinedashpattern: [Nsarray arraywithobjects:[nsnumber Numberwithint:3], [NSNum        ber Numberwithint:1],nil]];    Setup the path cgmutablepathref path = Cgpathcreatemutable ();    Cgpathmovetopoint (path, NULL, 0, 89);        Cgpathaddlinetopoint (Path, NULL, 320,89);    [Shapelayer Setpath:path];        Cgpathrelease (path); You can change self to any uiview you want, and the demo is put into UITableViewCell [[Self.view layer] addsublayer:shapelayer];} 

Example 6: Draw an arc

Draw an arc-(void) createcurvedline{    uibezierpath* apath = [Uibezierpath Bezierpath];    Apath.linewidth = 5.0;    Apath.linecapstyle = Kcglinecapround; Line corner    Apath.linejoinstyle = kcglinecapround;//end point processing    [Apath movetopoint:cgpointmake (+)];    [Apath Addquadcurvetopoint:cgpointmake (+) controlpoint:cgpointmake (0)];        Self. Curvedlinelayer=[cashapelayer layer];    Self. Curvedlinelayer.path=apath.cgpath;    [Self.view.layer addsublayer:self. Curvedlinelayer];}

Other than that.....

My wish is ...

World peace .....

iOS developing Bezier curves Uibezierpath (2)

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.