Dynamically draw curves on iOS

Source: Internet
Author: User

A feature that needs to be done in the near future is to dynamically finish drawing a curve on the screen. This curve can be used to complete the description of the data in a certain period of time changes and so on. This is probably the effect below.

How is this effect going to be done? These three classes need to be used Uibezierpath cashapelayer and cabasicanimation. Where Uibezierpath is used to draw the corresponding curve path, Cashapelayer is used to provide a location for path presentation, and the animation created by Cabasicanimation is added to the path.


First we create the path we want:

Uibezierpath *path = [Uibezierpath Bezierpath];    [Path Movetopoint:cgpointmake (0.0,20.0)];    [Path Addlinetopoint:cgpointmake (120.0, 500.0)]; [Path Addlinetopoint:cgpointmake (0)];    [Path Addlinetopoint:cgpointmake (310, +)];    [Path Addlinetopoint:cgpointmake (Screen_width, 110)];

We then create our own properties for Cashapelayer and assign our path to it. Without this assignment, the layer will not be able to draw this effect for us, and it is also an incomplete layer.


Cashapelayer *pathlayer = [Cashapelayer layer];    Pathlayer.frame = self.view.bounds;    Pathlayer.path = path. Cgpath;    Pathlayer.strokecolor = [[Uicolor redcolor] cgcolor];    Pathlayer.fillcolor = nil;    Pathlayer.linewidth = 2.0f;    Pathlayer.linejoin = Kcalinejoinbevel;        [Self.view.layer Addsublayer:pathlayer];

Finally, we assign the animation to our layer. In our animation effect, we can change some of the parameters to control its painting effect.

Cabasicanimation *pathanimation = [cabasicanimation animationwithkeypath:@ "Strokeend"];    Pathanimation = [cabasicanimation animationwithkeypath:@ "Strokeend"];    pathanimation.duration = 2.0;    Pathanimation.fromvalue = [NSNumber numberwithfloat:0.0f];    Pathanimation.tovalue = [NSNumber numberwithfloat:1.0f];    [Pathlayer addanimation:pathanimation forkey:@ "Strokeend"];

Now that we run the code, we can get the effect from the previous picture ~

Dynamically draw curves on iOS

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.