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

Source: Internet
Author: User

Http://www.cnblogs.com/GarveyCalvin/p/4193963.html

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:nil]; Start animation//Code ... [UIView commitanimations]; Submit Animation

Simple example:

[UIView Beginanimations:nil Context:nil]; Start animation [UIView setanimationduration:10.0]; Animation Duration/** *  image moves down */cgpoint point = _imageview.center;point.y + = 150;[ _imageview Setcenter:point]; [UIView commitanimations]; 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]; Cgpoint point = _imageview.center;point.y + = 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:

Cgcontextref 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 + (void) Beginanimations: (NSString *) Animationid context: (void *) context;//commit animation + (void) commitanimations; Sets the animation curve by default to constant speed: + (void) Setanimationcurve: (uiviewanimationcurve) curve;//set Animation duration: + (void) Setanimationduration: ( Nstimeinterval) duration; The default is yes. Skips the animation effect for no and jumps directly to the post-execution state. + (void) setanimationsenabled: (BOOL) enabled;//set animation delay execution (delay: seconds): + (void) Setanimationdelay: (Nstimeinterval) Delay Number of repetitions of the animation + (void) Setanimationrepeatcount: (float) repeatcount;//If yes, reverse (opposite) the animation effect, and return to the previous state of the animation after the end; Default is no:+ (void) setanimationrepeatautoreverses: (BOOL) repeatautoreverses;//set Animation agent: + (void) Setanimationdelegate: (ID) Delegate The animation will be executed at the beginning of the method XX (you must set the animation agent first): + (void) Setanimationwillstartselector: (SEL) selector;//Animation has ended when the execution method xx (must first set the animation agent): + ( void) Setanimationdidstopselector: (SEL) selector;/** * Set Animation transitions * * @param transitions for transition animations * @param view transition effect views * @ param cache If yes, the start and end views are rendered individually 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 cache: (BOOL) cache;//Delete all animation layers//Note: Layer refers to layout, example: [_imageview.layer removeallanimations];-(void) removeallanimations;

Second, the use of UIView animation block code:

Method One:

[UIView animatewithduration:4.0//Animation duration                 animations:^{                     //Code                 }];

Method Two:

[UIView animatewithduration:4.0//Animation duration                 animations:^{                     //code ...                 }                 completion:^ (BOOL finished) {                     //After the animation is complete                     //code ...                 }];

Method Three:

[UIView animatewithduration:4.0//Animation duration                      delay:2.0//Animation delay                    Options:uiviewanimationoptioncurveeasein//Animation transition effect                 animations:^{                     //code ...                 }                 completion:^ (BOOL finished) {                     //After the animation is complete                     //code ...                 }];

Method Four, Spring animationring Animation):

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

[UIView animatewithduration:4.0//Animation duration                      delay:0.0//Animation delay     usingspringwithdamping:1.0//Similar spring vibration effect 0~1      initialspringvelocity:5.0//initial speed                    Options:uiviewanimationoptioncurveeaseinout//Animation transition effect                 animations:^{                     Code ...                     Cgpoint point = _imageview.center;                     Point.y + =;                     [_imageview setcenter:point];                 } completion:^ (BOOL finished) {                     //animation after completion                     //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.

RPM: Spring Animation is an ideal alternative to linear or ease-out animations. Because the IOS itself is heavily used by Spring Animation, users are accustomed to the animated effect, so using it can make the App feel more natural, in Apple's words is "instantly familiar". In addition, Spring Animation is not only available for locations, 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 delay * @param options    animation effect option *  @ param animations Animation Execution code *  @param completion end of animation execution code */+ (void) Animatekeyframeswithduration: (Nstimeinterval) Duration                               delay: (nstimeinterval) Delay                             options: (uiviewkeyframeanimationoptions) options                          animations :(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     Animation execution Code */+ (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 color array nsarray *arraycolors = @[[uicolor Orangecolor],                              [Uicolor Yellowcolor], [Uicolor Greencolor], [Uicolor Bluecolor],    [Uicolor Purplecolor], [Uicolor Redcolor]];    Nsuinteger Colorcount = [arraycolors count]; Loop add Keyframe for (Nsuinteger i = 0; i < Colorcount; i++) {[UIView addkeyframewithrelativestarttime:i/(Cgfloa                                      T) colorcount Relativeduration:1/(cgfloat) Colorcount                                      animations:^{[_graduallyview Setbackgroundcolor:arraycolors[i]];    }]; }}; [UIView animatekeyframeswithduration:4.0 delay:0.0 Options:uivie Wkeyframeanimationoptioncalculationmodecubic |     Uiviewanimationoptioncurvelinear                     Animations:keyframeblock completion:^ (BOOL finished) { After the animation is done//code ...}];

The animation transition effect (Options) adds the following:

Uiviewkeyframeanimationoptioncalculationmodelinear     = 0 << 10,// Defaultuiviewkeyframeanimationoptioncalculationmodediscrete   = 1 << 10, uiviewkeyframeanimationoptioncalculationmodepaced      = 2 << 10, Uiviewkeyframeanimationoptioncalculationmodecubic      = 3 << 10, uiviewkeyframeanimationoptioncalculationmodecubicpaced = 4 << 10

Let's look at a diagram to make it easier to understand:

Summary:

There are many ways to animate uiview. Simple animation effect You can throw away, more complex animation effect you can choose the Keyframe Keyframe method.

As for which to choose, it is necessary to judge according to the product demand.

This article refers to the article:

Http://www.tuicool.com/articles/FjiQJbF

Http://www.tuicool.com/articles/ZR7nYv

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.