Objective-c Animation

Source: Internet
Author: User

want to improve the next to do the application to the customer to take the experience, so watched a few days OC Caanimation Animation class, also done a few small cases, the following individual cases to do as the main content of this article.

One: Inheritance structure

The most used classes above are probably, cabaseanimation and Cakeyframeanimation, the biggest difference between the two classes is the control of the animation, cabaseanimation can not add and control the frame in the class, Cakeyframeanimation This class can add and control frame animation and time per frame in the code, Caanimation is the base class for all classes, so it can implement functions that all classes can implement, and I use this class in my case. Caanimationgroup is a group animation that can animate another 5 animation class into its array of attributes and then add it to the layer in the target view of the animation, while achieving multiple animation effects.

Second: case

Here I have a total of two animated cases, the first one is Caaniamtion, the second is the Caanimationgroup group animation, it contains the

Cabaseanimation and Cakeyframeanimation

Caaniamtion Animation is written for the draw turntable, after winning the lottery from the bottom pop-up cue the winning animation view, the last frame of animation mainly to achieve the hidden view of the pop-up, x-coordinate = device width y coordinate =0

1: Package Animation method

-(caanimation*) Animation: (cgfloat) AxisX jumpvalue: (cgfloat) value Thistype: (int) type {cgmutablepathref path=cgpathcreatemutable ();      Cgpathmovetopoint (Path,null,axisx,device_height); if(type==0) {cgpathaddlinetopoint (path, NULL, AxisX,283); }    Else if(type==1) {cgpathaddlinetopoint (path, NULL, AxisX, the); }    Else if(type==2) {cgpathaddlinetopoint (path, NULL, AxisX, $); }    Else{cgpathaddlinetopoint (path, NULL, AxisX,283); } cgpathaddlinetopoint (Path, NULL, AxisX,179); Cgpathaddlinetopoint (Path, NULL, AxisX,207); Cgpathaddlinetopoint (Path, NULL, AxisX,187); Cgpathaddlinetopoint (Path, NULL, AxisX,199); Cgpathaddlinetopoint (Path, NULL, AxisX,193); Cgpathaddlinetopoint (Path, NULL, AxisX,195); Cgpathaddlinetopoint (Path, NULL, Device_width,0); Cakeyframeanimation*Animation=[cakeyframeanimation Animationwithkeypath:@"position"];    [Animation Setpath:path]; [Animation setduration:1.5];    [Animation setcalculationmode:kcaanimationlinear]; Nsarray* arr= [Nsarray arraywithobjects:[nsnumber numberwithfloat:0.0], [nsnumber numberwithfloat:0.12], [nsnumber numberwithfloat:0.24], [nsnumber numberwithfloat:0.36], [nsnumber numberwithfloat:0.48], [nsnumber numberwithfloat:0.60], [nsnumber numberwithfloat:0.72], [nsnumber numberwithfloat:0.5], [NSNumber numberwithfloat:value],nil];    [Animation Setkeytimes:arr]; [Animation Settimingfunctions:[nsarray arraywithobjects:[camediatimingfunction Functionwithname: Kcamediatimingfunctioneasein], [Camediatimingfunction functionwithname:kcamediatimingfun Ctioneaseout], [Camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaseo                                   UT], [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout],                                   [Camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaseout],                                   [Camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaseout], [Camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout], [ Camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaSeout], [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout], ni     L]]; Animation.Delegate=Self ; Animation.duration=1.5;    Cfrelease (path); returnanimation;}
View Code

2: Call

-(void) Viewwillappear: (BOOL) animated{[Super Viewwillappear:yes]; Coinviewarray=[[nsmutablearray Alloc]init];//This array is a field that holds the bottom pop-up view, and so the animation ends up needing to remove themCGFloat width=device_width- -; CGFloat EVERYWIDHT= (width- -)/4; CGFloat THEAXISX= -;  for(intI=0; i<4; i++) {Uiimageview*scview=[[uiimageview Alloc]initwithframe:cgrectmake (theaxisx+i* -+i*everywidht, Device_height, EVERYWIDHT, EVERYWIDHT)]; Scview.image=[uiimage imagenamed:@"coin.jpg"]; ScView.layer.cornerRadius=everywidht/2; Scview.backgroundcolor=[Uicolor Redcolor];        [Coinviewarray Addobject:scview];        CGFloat Jumpvalue; if(i==0) {Jumpvalue=1.2; }        Else if(i==1) {Jumpvalue=1.1; Scview.backgroundcolor=[Uicolor Yellowcolor]; }        Else if(i==2) {Scview.backgroundcolor=[Uicolor Bluecolor]; Jumpvalue=1; }        Else{Scview.backgroundcolor=[Uicolor Greencolor]; Jumpvalue=0.9;                            } [Scview.layer addanimation:[self animation:scview.layer.position.x Jumpvalue:jumpvalue thisType:i] Forkey:@"position"];                       [Scview.layer Setposition:cgpointmake (SCVIEW.FRAME.ORIGIN.X,SCVIEW.FRAME.ORIGIN.Y)];    [Self addsubview:scview]; } Uiimageview*imgview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Luckycenterbutton"]]; Imgview.backgroundcolor=[Uicolor Redcolor]; Imgview.frame= CGRectMake ( -, -, -, -); [Self Addsubview:imgview];}
View Code

3: Proxy (the view that is saved in the array after the animation finishes removing the animation)

-(void) Animationdidstop: (caanimation *) Anim finished: (BOOL) flag{    for (int  i=0; i<coinviewarray.count; i++) {        *imageview=[Coinviewarray objectatindex:i];                [ImageView Removefromsuperview];}    }
View Code

Caanimationgroup Active Draw, Bezier set the path of the Keyframe animation, the middle to achieve zoom, transparent .....

1: Code

-(void) Viewwillappear: (BOOL) animated{[Super Viewwillappear:yes]; Uiimageview*imgview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Luckycenterbutton"]]; Imgview.backgroundcolor=[Uicolor Redcolor]; Imgview.frame= CGRectMake ( -, -, -, -);                 [Self addsubview:imgview]; //Bézier curve PathUibezierpath *movepath =[Uibezierpath Bezierpath]; //set the start coordinates of an animation[Movepath Movetopoint:cgpointmake (device_width/2- -, device_height/2- -)];//, Cabasicanimation can be seen as a maximum of only 2 keyframes cakeyframeanimation           /* [_pushcovermudimviewcontroller.view.superview Addsubview:_customactionsheet];     Cakeyframeanimation *popanimation = [cakeyframeanimation animationwithkeypath:@ "transform"];     Popanimation.duration = 0.4; Popanimation.values = @[[nsvalue Valuewithcatransform3d:catransform3dmakescale (0.01f, 0.01f, 1.0f)], [Nsvalue valueWi Thcatransform3d:catransform3dmakescale (1.1f, 1.1f, 1.0f)], [Nsvalue Valuewithcatransform3d:catransform3dmakescale (     0.9f, 0.9f, 1.0f)], [Nsvalue valuewithcatransform3d:catransform3didentity]];     Popanimation.keytimes = @[@0.2f, @0.5f, @0.75f, @1.0f]; Popanimation.timingfunctions = @[[camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaseout], [ Camediatimingfunction Functionwithname:kcamediatimingfunctioneaseineaseout], [camediatimingfunction     Functionwithname:kcamediatimingfunctioneaseineaseout]];     [_customactionsheet.layer addanimation:popanimation Forkey:nil]; */[Movepath addquadcurvetopoint:cgpointmake (Device_width,0) ControlPoint:self.view.center]; //The following must import the Quartzcore package//keyframe Animation (position)Cakeyframeanimation * Posanim = [cakeyframeanimation animationwithkeypath:@"position"]; Posanim.path=Movepath.cgpath; Posanim.removedoncompletion=YES; //Zoom AnimationCabasicanimation *scaleanim = [cabasicanimation animationwithkeypath:@"Transform"]; Scaleanim.fromvalue=[Nsvalue valuewithcatransform3d:catransform3didentity]; //zoom in and out when animatingScaleanim.tovalue = [Nsvalue Valuewithcatransform3d:catransform3dmakescale (0.3,0.3,1)]; Scaleanim.removedoncompletion=YES; //Transparent animationsCabasicanimation *opacityanim = [cabasicanimation animationwithkeypath:@"Alpha"]; Opacityanim.fromvalue= [NSNumber numberwithfloat:1.0]; Opacityanim.tovalue= [NSNumber numberwithfloat:0.1]; Opacityanim.removedoncompletion=YES; //Animation GroupCaanimationgroup *animgroup =[Caanimationgroup animation]; Animgroup.animations=[Nsarray Arraywithobjects:posanim, Scaleanim, Opacityanim, nil]; Animgroup.duration=Ten; //The two properties of the animation need to be set so that the layer stays behind the animation after the animation is finished. AAnimgroup.fillmode =kcafillmodeforwards; Animgroup.removedoncompletion=NO; [Imgview.layer Addanimation:animgroup Forkey:nil]; }
View Code

Objective-c Animation

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.