IPhone Development Study Notes: Using uiview for animation Effects

Source: Internet
Author: User

Uikit encapsulates core animation to implement some commonly used animation effects, which is very convenient to use. You can use uiview to declare an animation block. Any attribute changes made in this block will render the animation effect.

There are two types of syntax. Here we talk about the old-fashioned method. For the new method after ios4.0, refer to the document. The basic idea is the same. For details, refer to this:
Http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html

First, describe several basic concepts to facilitate understanding of the subsequent functions.
Attribute change: attributes that can realize the animation effect include the position (frame, bound), alignment, transparency, background color, content stretching, and transform (this is more, as described below)
Timing curve: The time curve, with time as the horizontal axis, and other values (here referred to as the attribute to be changed) as the vertical axis. The function curve during the animation duration.
Slow in/slow out: slow in/slow out. Combined with the above concept of time curve, attribute changes will slow down at the animation start/end. See the figure below: it is animated in seek out, which is also the default animation effect (not very good, just find it online)

Liner: linear variation. This is not to mention. There are two kinds of time variation curves. The default value is easeineaseout. Undoubtedly, the effect of easeineaseout will be smoother, but the load will be higher, but it is generally not a problem.
Fade in/fade out: fade in, fade out, is an animated effect, that is, gradually disappear, and such a thing gradually appears.

Before talking about specific functions, let's take an example first,

Code

  [Uiview beginanimations:  @"  Toggleviews  "  Context: Nil];

[Uiview setanimationduration: 1.0 ];
[Uiview setanimationcurve: uiviewanimationeaseinout];
// Make the animatable changes.
Firstview. Alpha = 0.0 ;
Secondview. Alpha = 1.0 ;
// Commit the changes and perform the animation.
[Uiview commitanimations];

This sectionCodeYou can implement a nice fade-in and fade-out switchover. All you need to do is use the begin/commit function to circle a region and write the changes you want to make into it, no matter how many, they will not be immediately executed, knowing that the commit function is committed. A simple description of the function:
Beginanimation: Context: Both parameters are used for delegate. Generally, Nil is fine. The animationid indicates the name of the current animation. It is used for different purposes when a proxy corresponds to a multi-terminal animation, the context is void *, which is commonly used in callback functions. It is used to transmit additional data, save the context, and avoid using global variables.
Setanimationcurve: As mentioned above, the default value is uiviewanimationcurveeaseinout. You can leave it empty.
Setanimationduration: Specifies the animation length, in seconds.

Add another common function, setanimationrepeatcount: You can repeat the animation, which is quite useful in some scenarios.
If you need to perform operations before or after an animation, you can define a proxy (that is, two callback functions ). Let's take a look at the example below. An animation is followed by another animation. If you are familiar with the use of the agent, you will not be able to talk about it.

Code

  // This method begins the first animation.  

- (Ibaction) showhideview :( ID) sender

{

[Uiview beginanimations: @" Showhideview " Context: Nil];

[Uiview setanimationcurve: uiviewanimationcurveeasein];

[Uiview setanimationduration: 1.0 ];

[Uiview setanimationdelegate: Self];

[Uiview setanimationdidstopselector: @ selector (showhidedidstop: Finished: context :)];



// Make the animatable changes.

Thirdview. Alpha = 0.0 ;



// Commit the changes and perform the animation.

[Uiview commitanimations];

}



// Called at the end of the preceding animation.

- ( Void ) Showhidedidstop :( nsstring * ) Animationid finished :( nsnumber * ) Finished context :( Void * ) Context

{

[Uiview beginanimations: @" Showhideview2 " Context: Nil];

[Uiview setanimationcurve: uiviewanimationcurveeaseout];

[Uiview setanimationduration: 1.0 ];

[Uiview setanimationdelay: 1.0 ];



Thirdview. Alpha = 1.0 ;



[Uiview commitanimations];

}

 

Next we will talk about the view transition animation effect, which is a common page flip effect. The old version is written like this:

Code

  //Code Excerpt from basic iPhone Development
[Uiview setanimationtransition:
Uiviewanimationtransitionflipfromright
Forview: Self. View cache: Yes];

[Blueviewcontroller viewwillappear: Yes];
[Yellowviewcontroller viewwilldisappear: Yes];
[Blueviewcontroller. View removefromsuperview];
[Self. View insertsubview: yellowviewcontroller. View atindex:0];
[Yellowviewcontroller viewdiddisappear: Yes];
[Blueviewcontroller viewdidappear: Yes];

 

Before ios4.0, to achieve the animation effect switching between views, you must use the parent view and then switch the child view. Only the effect of the Child view can be animated, so you can see the parent view written in forview in setanimationtranistion.

After 4.0, you can write it like this:

Code

 
  [Uiview transitionfromview :( self. View)
Toview :( self. _ view2)
Duration:1.0
Options: uiviewanimationoptiontransitioncurlup
Completion:^(Bool finished ){
}
];

 

 

 

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.