How to implement the animation effect of iOS development-uiview (collection)

Source: Internet
Author: User

Introduction: In the development of the app, we will often use the animation effect. Using animations can make our apps cooler and more glamorous, and most importantly, optimizing the user experience, but depending on the quality of the animation. Like QQ, Sina Weibo and other apps, animation effect is very good, at least I like their animation, let me use to feel very smooth, very cheerful mood. This article describes the implementation of the UIView effect, non-core animation.

First, using the UIView class to implement the animation

The basic notation, the code must be placed between Begin and commit:

[UIView beginanimations:Nil Context:// start animation // Code ... [// Submit animation       

Simple example:

[UIView beginanimations:Nil Context:// start animation [UIView setanimationduration:// animation duration/ * */150;[ _imageview Setcenter:point]; [// Submit animation           

Run multiple animation effects at the same time:

[UIView beginanimations:nil context:nil];[ UIView Setanimationduration:3.0]; [_imageview Setalpha:0.0]; [UIView commitanimations]; [UIView beginanimations:nil context:nil];[ UIView Setanimationduration:3.0]; 150; [_imageview Setcenter:point]; [UIView commitanimations];             

The above code implements the animation effect (simultaneous execution):

1. The image is shifted downward by 150 pixels

2, set the image transparency to 0.

Specify the context:

Uigraphicsgetcurrentcontext (); [UIView beginanimations:nil Context:context]; [UIView setanimationduration:2.0]; [_imageview Setalpha:0]; [UIView commitanimations];     

Uigraphicsgetcurrentcontext (): Gets the context of the current view

Other methods and properties:

The following methods and properties are not all, only examples (other methods and properties not mentioned) Please try it yourself, thank you):

//Start Animation + (voidBeginanimations: (NSString *) AnimationidContext: (void *) context;//Submit Animation + (void) commitanimations;//The animation curve is set by default at a constant speed: + (voidSetanimationcurve: (Uiviewanimationcurve) curve;//Set Animation Duration: + (voidSetanimationduration: (nstimeinterval) duration;//The default is yes. Skips the animation effect for no and jumps directly to the post-execution state. + (voidSetanimationsenabled: (BOOL) enabled;//Set animation delay Execution (delay: seconds): + (voidSetanimationdelay: (nstimeinterval) delay;//Number of repetitions of the animation + (voidSetanimationrepeatcount: (float) RepeatCount;//If yes, the reverse (opposite) animation effect, the end of the animation before returning to the state of the reverse; The default is no:+ (voidSetanimationrepeatautoreverses: (BOOL) repeatautoreverses;//Set the animation agent: + (voidSetanimationdelegate: (id) delegate;//The animation will be executed at the beginning of the method XX (you must set the animation agent first): + (voidSetanimationwillstartselector: (SEL) selector;// Execute method XX When the animation is finished (you must set the animation agent first): + ( void) setanimationdidstopselector: (SEL) selector; /** * Set animation transitions *  @param transition Animation transition effect *  @param view transition effect View *  @param cache if yes, The start and end views are rendered once and frames are created in the animation, otherwise the view renders each frame. For example, you don't need to keep updating in view transitions, you just have to wait until the conversion is complete before you update the view. */+ (void)  Setanimationtransition: (uiviewanimationtransition) transition forview: (UIView *) view Span class= "hljs-string" >cache: (BOOL) cache; // Delete all animation layers // Note: The layer refers to layout, example: [_imageview.layer removeallanimations];-(void) removeallanimations;  
Second, the use of UIView animation block code:

Method One:

Animatewithduration:// animation duration                  animations:^{                     // code}];    

Method Two:

[UIView animatewithduration://///code ... } completion:^ (//// Code ...}];   

Method Three:

[UIView animatewithduration:// animation duration  :// Animation delay Options://// Code ...} completion:^ (//// Code ...}];          

Method Four, Spring animationring Animation):

At the beginning of IOS7, the system animation effect is widely applied to spring Animation:

[UIViewAnimatewithduration:4.0//Animation durationDelay0.0//Animation delayUsingspringwithdamping:1.0// similar to spring vibration effect 0~1 Initialspringvelocity:5.0 // Initial speed options:uiviewanimationoptioncurveeaseinout // animation transition effect  animations:^{// code ... Cgpoint point = _imageview.center; Point.y + = 150; [_imageview setcenter:point];} completion:^ (BOOL finished) {// After the animation is complete // code ... [_imageview setalpha:1];}];      

Usingspringwithdamping: It has a range of 0.0f to 1.0f, the smaller the value, the more obvious the vibration effect of "spring".

Initialspringvelocity: initial speed, the higher the number starts to move faster. It is worth noting that the initial speed value is higher and the time is short, there will also be rebound situation.

Animation is not available for location only, it applies to all properties that can be animated.

Method Five, Keyframe animation:

UIView animations already have advanced methods to create animations and can better understand and build animations. After IOS7 Apple added a new Animatekeyframeswithduration method, we can use it to create more complex and cool animation effects without the need to use the Core animation (Coreanimatino).

To create a Keyframe method:

/** * Add Keyframe method * *  @param duration Animation Duration *  @param delay Animation delayed *  @param options Animation effect option *  @param animations Animation execution code *  @param comple tion animation End Execution Code */+ (void)  Animatekeyframeswithduration: (nstimeinterval) Duration delay: (nstimeinterval) delay< Span class= "Hljs-symbol" > options: (uiviewkeyframeanimationoptions) Options animations: ( Span class= "Hljs-keyword" >void (^) (void)) Animations Completion: (void (^) (BOOL finished)) completion;       

To add a Keyframe method:

/* *  Add keyframes * *  @param framestarttime Animation relative start time *  @param frameduration  Animation relative duration *  @ param animations     */+ (void)addkeyframewithrelativestarttime: (double) Framestarttime  Relativeduration: (double) frameduration animations: (void (^) (void)) animations;    

The relative time mentioned above, that is to say: "They will automatically match the duration of the animation according to the length of its running time."

Here is a simple example to answer, the rainbow Change View:

void (^keyframeblock) () = ^ () {//Create a color arrayNsarray *arraycolors = @[[Uicolor Orangecolor], [Uicolor Yellowcolor], [Uicolor Greencolor], [Uicolor Bluecolor], [Uicolor Purplecolor], [Uicolor Redcolor]];Nsuinteger Colorcount = [arraycolors count];//Looping add keyframesfor (nsuinteger i = 0; i < Colorcount; i++) {[uiview addkeyframewithrelativestarttime:i/(CGFloat) colorCount Relativeduration:1/(cgfloat) Colorcount animations:^{[_ Graduallyview Setbackgroundcolor:arraycolors[i]]; }]; }}; [uiview animatekeyframeswithduration:4.0 Delay:0.0 options:uiviewkeyframeanimationoptioncalculationmodecubic | uiviewanimationoptioncurvelinear animations:keyframeblock completion:^ ( bool finished) {// After the animation is complete // code ...}];              

The animation transition effect (Options) adds the following:

Uiviewkeyframeanimationoptioncalculationmodelinear     ,//default  Uiviewkeyframeanimationoptioncalculationmodediscrete, ten, ten   ,    Ten 

How to implement the animation effect of iOS development-uiview (collection)

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.