iOS Animation Implementation Summary

Source: Internet
Author: User

In iOS, there are two kinds of animation, one is the animation method of operation UIView, the other is the core animation, but in IOS7, UIView is involved with the core animation.

Mode one (animating with the core animation)
    • The hierarchical relationship of the core animation

    • Transition Animation (catransition)

      • A conversion animation for a scene that can occasionally animate a layer to move out of the screen and as if it were a screen.
      • Uinavigationcontroller is through the catransition realization of the controller's master and pupil pushed into the screen animation effect.
      • Common Properties
        • Type animation over types
        • Subtype: Animation over direction
        • Startprogress: Start of animation (percent in total) (the available values from 0 to 1, the time of the start or end of the animation, the starting time must be smaller than the end time, the same as below)
        • Endprogress: End of Animation (percentage in overall animation)
   Catransition *animation = [catransition animation];     @" reveal " ;     1 ;     = kcatransitionreveal;    [Self.myView.layer addanimation:animation Forkey:nil];     = Self.myView.center;      Max ;    [Self.myview Setcenter:point];

  • Basic animation (cabasicanimation), is a subclass of Capropertyanimation, an animation can control the change of a property, the change value can only be two values, can be set in Fromvalue and Tovalue two values

    Cabasicanimation *baseanimation = [cabasicanimation animationwithkeypath:@"bounds"]; Baseanimation.fromvalue= [Nsvalue Valuewithcgrect:cgrectmake ( -, -, -, -)]; Baseanimation.tovalue= [Nsvalue Valuewithcgrect:cgrectmake ( -, -, $, $)]; Baseanimation.duration=2.0; Baseanimation.removedoncompletion=NO; Baseanimation.fillmode=kcafillmodeforwards; Baseanimation.repeatcount=maxfloat; [Self.myView.layer addanimation:baseanimation Forkey:nil];

  • Frame animation (cakeyframeanimation), frame animation is also a subclass of Capropertyanimation, so it is also the control of a view of the properties of animation, unlike Cabaseanimation, Cakeyframeanimation can add multiple keyframes, and cabaseanimation can be seen as a frame animation of two keyframes, and we can take advantage of frame animation keyframes to make more interesting animations, such as bubble effects.

Caanimationgroup *group =[[Caanimationgroup alloc] init];//DisplacementCakeyframeanimation *positionanima = [cakeyframeanimation animationwithkeypath:@"position"]; Positionanima.calculationmode=kcaanimationcubicpaced; Positionanima.duration= 5; Positionanima.fillmode=kcafillmodeforwards; Positionanima.removedoncompletion=NO; Positionanima.repeatcount=maxfloat; Positionanima.timingfunction=[Camediatimingfunction functionwithname:kcamediatimingfunctionlinear];//Add a move pathCgmutablepathref Curvedpath =cgpathcreatemutable (); CGRect Circlecontainer= Cgrectinset (Myview.frame, MyView.frame.size.width/2-3, MyView.frame.size.height/2-3);    Cgpathaddellipseinrect (Curvedpath, NULL, Circlecontainer); Positionanima.path=Curvedpath;    Cgpathrelease (Curvedpath); [Myview.layer Addanimation:positionanima Forkey:nil];//Zoom xCakeyframeanimation *scalex = [cakeyframeanimation animationwithkeypath:@"transform.scale.x"]; Scalex.duration=1.0; Scalex.values= @[@1.0,@1.1,@1.0]; Scalex.keytimes= @[@0.0,@0.5,@1.0]; Scalex.repeatcount=maxfloat; Scalex.autoreverses=YES; Scalex.timingfunction=[Camediatimingfunction functionwithname:kcamediatimingfunctioneaseout]; [Myview.layer Addanimation:scalex Forkey:nil];//Zoom yCakeyframeanimation *scaley = [cakeyframeanimation animationwithkeypath:@"Transform.scale.y"]; Scaley.duration=1.5; Scaley.values= @[@1.0,@1.1,@1.0]; Scaley.keytimes= @[@0.0,@0.5,@1.0]; Scaley.repeatcount=maxfloat; Scaley.autoreverses=YES; Group.animations=@[positionanima,scalex,scaley]; Scaley.timingfunction=[Camediatimingfunction functionwithname:kcamediatimingfunctioneaseout]; [Myview.layer Addanimation:scaley Forkey:nil];

Run effect

Because my computer is a black apple, so will be a bit, haha, white Apple should not be like this.

    • Animation Group (Caanimationgroup) Caanimation subclass, you can save a set of animated objects, say Caanimationgroup object after the join layer, all the animation in the group can run simultaneously, so when we need to do more than the animation and execution time is different , the animation group is not applicable. For example, the bubble effect above.
Group.animations = [animate object inside];

Mode two (add animation with UIView)
    • UIView Animation (hand code)
      • Add a single animation
         [uiview Beginanimations:nil Context:nil]; [UIView setanimationduration:  4   = Self.myview.center;point.y  + = 150    

         

      • add multiple animations
[UIView Beginanimations:nil context:nil];    [UIView setanimationduration:    4];     = Self.myView.center;      Max ;    [Self.myview Setcenter:point];    [UIView commitanimations];    [UIView Beginanimations:nil Context:nil];    [UIView setanimationduration: 4 ];    [Self.myview Setalpha: 0.1 ];    [UIView commitanimations];

    • UIView Animation (Block)
[UIView animatewithduration:4 animations:^{        = self.myView.center;          Max ;        [Self.myview setcenter:point];    }];

    • UIView Animation (block frame animation), opened from IOS7, Apple provides a more convenient way to invoke frame animation, without creating a new frame animation instance, directly control the properties of the layer.
 [uiview animatekeyframeswithduration:0.5  delay:1  options:uiviewkeyframeanimationoptionautoreverse animations: ^{self.view.bounds  = CGRectMake (30 , 
    
     30 
    , 30 , 30  ); } Completion:  ^  
 
    • UIView transitions Animation.
+ (void) Transitionfromview: (UIView *) Fromview Toview: (UIView *) Toview Duration: (nstimeinterval) duration Options: (uiviewanimationoptions) Options completion: (void (^) (BOOL finished)) completion

This method should not be easy to understand, simply put, after this method is called, the equivalent of executing two lines of code,

// Add Toview to Parent view [Fromview.superview Addsubview:toview]; // Remove the Fromview from the parent view [Fromview.superview Removefromsuperview]; - Duration: Duration of animation - Duration: Duration of animation - options: Type of transition animation -  Animations: Place code that alters the view properties in this block -completion: The block is automatically called when the animation is finished

 

iOS Animation Implementation Summary

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.