Ios-uses Uibezierpath and caanimation to make path animations

Source: Internet
Author: User

following the last article of the heartbeat animation, today to implement a path based on the heartbeat path animation, so that a view along the path of movement.

Core Code 1-First draw the heart-shaped path through the DrawRect

-(void) DrawRect: (cgrect) Rect {//Drawing code//initialization Uibezierpath Uibezierpath *path = [Uibezierpath bezierpa
    TH];
    First set a starting point cgpoint StartPoint = Cgpointmake (RECT.SIZE.WIDTH/2, 120);
    Starting point as the path to the start [path Movetopoint:startpoint];
    Set an endpoint Cgpoint EndPoint = Cgpointmake (RECT.SIZE.WIDTH/2, rect.size.height-40);
    Set the first control point cgpoint controlPoint1 = Cgpointmake (100, 20);
    Set the second control point cgpoint controlPoint2 = Cgpointmake (0, 180);
    Add three Bezier curves [path addcurvetopoint:endpoint controlpoint1:controlpoint1 Controlpoint2:controlpoint2];
    Set another starting point [path movetopoint:endpoint];
    Set the third control point cgpoint ControlPoint3 = Cgpointmake (rect.size.width-100, 20);
    Set fourth control point Cgpoint controlPoint4 = Cgpointmake (rect.size.width, 180);
    Add three Bezier curves [path addcurvetopoint:startpoint controlpoint1:controlpoint4 Controlpoint2:controlpoint3];
    Set line width path.linewidth = 3; Set line section type Path.linecapstyle = Kcglinecapround
    Set Connection Type Path.linejoinstyle = Kcglinejoinround;
    Sets the brush color [[Uicolor Redcolor] set];
[Path stroke]; }
2-Add Heart path to view
    Heartview *heart = [[Heartview alloc] init];
    Heart.frame = CGRectMake (0, 0, screen_width, screen_height-screen_height);
    [Self.view Addsubview:heart];
3-Add trajectory path animation to animated view (red circle view)
    Cakeyframeanimation *animation = [cakeyframeanimation animationwithkeypath:@ "position"];
    Set the path of the animation to the heart-shaped path
    Animation.path = Self.path.CGPath;
    Animation time interval
    animation.duration = 3.0f;
    The number of repetitions is the maximum
    animation.repeatcount = Flt_max;
    Animation.removedoncompletion = NO;
    Animation.fillmode = kcafillmodeforwards;
    Add an animation to the animated view
    [_demoview.layer addanimation:animation Forkey:nil];
Related Article

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.