iOS Basic Animation tutorial

Source: Internet
Author: User

iOS animation a variety of good applications will be more attractive, it will be more dazzling, this article introduces several basic iOS animation, a single explanation is easy to understand, but the real use, combined with will look more handsome, this depends on the specific application scenario and everyone's imagination.

All the underlying animations give UIView a basic method: Animatewithduration. This method can contain a block of code that sets what is to be changed, and iOS is automatically animated as it executes, with the following code:

[UIView animateWithDuration:1 animations:^{        // 要执行的动作}];

The parameter "1" indicates that the animation is completed in a second time.
Now explain the animation of position, transparency, size, color, rotation, respectively.

Position animation

We put a block on the interface, and then we want him to move through the animation to another location, how to do it? It's easy to change the center of the block at the top of the code block, as follows:

 [UIView animatewithduration:1  animations:^{//Change the position of the blue block cgpoint bluecenter = Self.bluesquare  .center  ;//get the original block center position  Bluecenter.x  = Self.view   .size  .width -self< Span class= "Hljs-preprocessor" >.bluesquare  .center  .x  ;//change the x-coordinate of the center position  Self .bluesquare  .center  = Bluecenter;//set the center position of the block to the new location }]   

This way you can see the animation, very simple.
In addition, you can delay the execution time of the animation, such as want to delay after half a second to execute, then the same method, but a little more parameters:

[UIViewAnimatewithduration:1Delay0.5OptionsNilanimations:^{//Change the position of the blue block        CgpointBluecenter = Self. Bluesquare. Center;//Get the original block center positionBluecenter. x= Self. View. Bounds. Size. Width- Self. Bluesquare. Center. x;//change the x-coordinate of the center position         Self. Bluesquare. Center= Bluecenter;//Set the center position of the block to the new location} Completion:Nil];

The delay parameter indicates that the animation is performed in 0.5 seconds, the options can not be filled, the completion is the finished operation, or it can not be filled. This is achieved.

Transparency Animations

Let's say we want to animate the transparency of a control, such as slowly becoming basically invisible, and very simple, or that way:

    // 开始透明度动画(一秒完成)    [UIView animateWithDuration:1 animations:^{        // 透明度变为0.1        self.blueSquare.alpha0.1;    }];

The original box has transparency, of course, the default is 1, through this setting, you can let it in a second time slowly turn the transparency into 0.1, is not very simple!

Size animation

If you want to change the size of a control, you need to use a change in the size of the code block function: Cgaffinetransformmakescale, the function of the parameters for the set length and width of the original number of times, for example, we use the animation to enlarge the control to twice times the original:

    // 进行一秒钟的动画    [UIView animateWithDuration:1 animations:^{        self.blueSquare.transform = CGAffineTransformMakeScale(2.02.0);// 长和宽分别变成原来的两倍    }];

Here the block through the animation of a second slowly magnified to the original twice times, here to be clear is the amplification process, the center of the block is unchanged, that is, from the center to the surrounding zoom. To zoom out, you can also change the multiple of the parameter.
Here's a little bit of imagination, we're going to combine the magnified animation with the Transparency animation, zoom in to the entire screen edge gradient to invisible, is it just like some of the animations we've seen?

Color animation

Now comes the color of the gradient animation, the same simple very:

    // 改变颜色    [UIView animateWithDuration:1 animations:^{        self.blueSquare.backgroundColor = [UIColor redColor];    }];

In the code block to reset the color of the box, you can achieve the gradient effect, simple to cry ...

Rotate animation

The animation above is very simple, it is in the animation of the code block reset can achieve the effect of animation, and the rotation is a little more complicated.
Suppose we have a picture of a wheel wheelimg, to rotate him, or need to use the method cgaffinetransformmakerotation, just now we use the size of the telescopic Cgaffinetransformmakescale, looks similar, The parameter is the angle of rotation, we can try to write:

    [UIView animateWithDuration:1 animations:^{        self.wheelImg.transform = CGAffineTransformMakeRotation(M_PI);    }];

This can really achieve the purpose of rotation, according to the parameters, the running time will be rotated semicircle, and then stop. If you just want to spin it and stop, write it this way, change the angle, but if you want to rotate a whole circle, the first thought might be to change the angle to the full circle:

    [UIView animateWithDuration:1 animations:^{        self.wheelImg.transform = CGAffineTransformMakeRotation(2*M_PI);    }];

This writing, the operation is actually not moving, you can try it, because its final position, that is, to turn a full circle, or in the original position, so iOS choose not to move. It doesn't move as you change position, position or original position. What to do then. In addition, the rotation here is a one-time, if you want to always turn, how to do it, is it easy to think of loops? Actually is the loop, but we can use more elegant than for loop animation Loop way, remember just do delay animation when the method, and finally a parameter is completion, the function of this parameter is to provide the animation at the end of the execution of the content, then can we call it ourselves here? Of course:

//continuous rotation animation -(void ) spin {//options property settings allow it to rotate smoothly, completion let it continue to call itself after completion  [uiview  animatewithduration:1  delay:< Span class= "Hljs-number" >0  options:uiviewanimationoptioncurvelinear animations:^{self  .wheelimg  .transform  = Cgaffinetransformrotate (self  .wheelImg  Span class= "hljs-variable" >.transform , M_PI); //the first argument is the angle at which the rotation starts, the second is the angle of rotation } completion:^ (bool  finished) {//continue execution at the end of  [    Span class= "Hljs-keyword" >self  spin]; }];}

Here we put the animation into a function, convenient for us to call in the completion, so that the implementation of continuous rotation, of course, if you want to only rotate a whole circle, you can use for the loop, all roads to Rome.

The above is the basic iOS UIView animation, a single look at each is quite simple, in our real use, of course, we should pay attention to the use of combination, play the imagination, simple function is also can be combined out handsome effect ~

Can download my sample project on GitHub: Https://github.com/Cloudox/iOSAnimationSample
All rights reserved: Http://blog.csdn.net/cloudox_

iOS Basic Animation tutorial

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.