Detailed usage of iOS development--cakeyframeanimation

Source: Internet
Author: User

Simple to create a path with the animation effect, relatively coarse, but the principle is the same in advance,

The code is as follows:

1, create the view required for the animation

-(void) creatview{    new];         = [Uicolor purplecolor];         = CGRectMake (00);        [Self.view Addsubview:moveview];}

2, the implementation of the animation:

Method One:

-(void) startanimation{//method one usage 1? Value Method//Create an animated objectcakeyframeanimation*animation = [Cakeyframeanimation animationwithkeypath:@"position"]; //Set ValueNsvalue*value1=[nsvalue Valuewithcgpoint:cgpointmake ( -, -)]; Nsvalue*value2=[nsvalue Valuewithcgpoint:cgpointmake ( $, -)]; Nsvalue*value3=[nsvalue Valuewithcgpoint:cgpointmake ( $, $)]; Nsvalue*value4=[nsvalue Valuewithcgpoint:cgpointmake ( -, $)]; Nsvalue*value5=[nsvalue Valuewithcgpoint:cgpointmake ( -, -)]; Nsvalue*value6=[nsvalue Valuewithcgpoint:cgpointmake ( $, -)]; Animation.values=@[value1,value2,value3,value4,value5,value6]; //repeat number defaults to 1Animation.repeatcount=maxfloat; //sets whether the original path is returned by default to notanimation.autoreverses=YES; //set the movement speed, the smaller the fasteranimation.duration=4.0f; Animation.removedoncompletion=NO; Animation.fillmode=kcafillmodeforwards; Animation.timingfunction=[Camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; Animation.Delegate=Self ; //Add animation to this view[Moveview.layer addanimation:animation forkey:nil];}

Method Two:

//usage 2? Path Mode//Create an animated objectcakeyframeanimation*animation = [Cakeyframeanimation animationwithkeypath:@"position"]; //Create a Cgpathref object that is the route of the animationcgmutablepathref Path=cgpathcreatemutable (); //automatically moves along radianscgpathaddellipseinrect (Path, NULL, CGRectMake ( Max, $, $, -)); //Set Start positionCgpathmovetopoint (Path,null, -, -); //move along a straight lineCgpathaddlinetopoint (Path,null, $, -); Cgpathaddlinetopoint (Path,null, $, $); Cgpathaddlinetopoint (Path,null, -, $); Cgpathaddlinetopoint (Path,null, -, -); Cgpathaddlinetopoint (Path,null, $, -); //move along a curveCgpathaddcurvetopoint (Path,null,50.0,275.0,150.0,275.0,70.0,120.0); Cgpathaddcurvetopoint (Path,null,150.0,275.0,250.0,275.0,90.0,120.0); Cgpathaddcurvetopoint (Path,null,250.0,275.0,350.0,275.0,110.0,120.0); Cgpathaddcurvetopoint (Path,null,350.0,275.0,450.0,275.0,130.0,120.0); Animation.path=path;        Cgpathrelease (path); Animation.autoreverses=YES; Animation.repeatcount=maxfloat; Animation.removedoncompletion=NO; Animation.fillmode=kcafillmodeforwards; Animation.duration=4.0f; Animation.timingfunction=[Camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; Animation.Delegate=Self ; [Moveview.layer addanimation:animation Forkey:nil];
}

According to the above method, you can implement an animation, referring to the above logic to achieve the addition of shopping cart, delete and other paths with the animation!

The following is an animated effect of adding a shopping cart:

1. Create an animated view

-(void) relodata{    = [[Uiimageview alloc] Initwithframe:cgrectmake (self.view.frame.size.width /4, self.view.frame.size.height+)];     true ;    [Self.view addSubview:self.addCartImg];     = [UIImage imagenamed:@ "3.jpg"];}

2, the implementation of the specific animation:

-(Ibaction) CLICKADDSHOPCARTBTN: (ID) Sender {Self.addCartImg.hidden=false;//Create an animated objectCakeyframeanimation *keyframeanimation=[cakeyframeanimation Animationwithkeypath:@"position"];//Create a Cgpathref object that is the route of the animationCgmutablepathref Path =cgpathcreatemutable ();//Set Start positionCgpathmovetopoint (Path, NULL, self.addcartimg.layer.position.x- +, self.addcartimg.layer.position.y- +);//move to start point//add a four curve point movement along a pathCgpathaddquadcurvetopoint (Path, NULL, -, -, Self.view.frame.size.width,0); Keyframeanimation.path=path; Keyframeanimation.Delegate=Self ;    Cgpathrelease (path); Keyframeanimation.duration=0.7; [Self.addCartImg.layer addanimation:keyframeanimation Forkey:@"kckeyframeanimation_position"]; //Rotate Animationcabasicanimation*rotationanimation; Rotationanimation= [Cabasicanimation Animationwithkeypath:@"transform.rotation.z"]; Rotationanimation.tovalue= [NSNumber Numberwithfloat:m_pi *2.0 ]; Rotationanimation.duration=0.1; Rotationanimation.cumulative=YES; Rotationanimation.repeatcount= -; //Add a rotation animation to addcartimg[Self.addCartImg.layer addanimation:rotationanimation Forkey:@"rotationanimation"];}

Click on the button, ADDCARTIMG will do a rotation operation, and follow the specified path to move, so as to complete an animation!

Detailed usage of iOS development--cakeyframeanimation

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.