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.
MySQL view1.alpha = 0.0;
MyView2.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
This code can implement a nice fade-in and fade-out switch. All you need to do is use the begin/commit function to circle a region and write the changes you want to make, 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.
The Code is as follows:
-(Void) btn1Pressed
{
[UIView beginAnimations: @ "ShowHideView1" context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: @ selector (showHideDidStop: finished: context :)];
// Make the animatable changes.
M_myView1.alpha = 0.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
}
-(Void) btn2Pressed
{
[UIView beginAnimations: @ "ShowHideView2" context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: @ selector (showHideDidStop: finished: context :)];
// Make the animatable changes.
M_myView2.alpha = 0.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
}
-(Void) showHideDidStop :( NSString *) animationID finished :( NSNumber *) finished context :( void *) context
{
If (animationID = @ "ShowHideView1 ")
{
[UIView beginAnimations: @ "ShowHideView1" context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelay: 2.0];
M_myView1.alpha = 1.0;
[UIView commitAnimations];
}
Else if (animationID = @ "ShowHideView2 ")
{
[UIView beginAnimations: @ "ShowHideView2" context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationDelay: 2.0];
M_myView2.alpha = 1.0;
[UIView commitAnimations];
}
}