Animation is actually a change in a period of time, which implies that the change must be at a certain rate. The rate is calculated from the following formula:
velocity = change / time
The change here can refer to the distance that an object moves, and time refers to the duration of the animation. Using such a movement can be more visually described (such as the animation of the position and bounds properties), but in fact it can be applied to any animation. Attributes (such as color and opacity).
The above equation assumes that the speed is constant throughout the animation process (as in the case of Chapter 8 "explicit animation"). For this constant speed animation we call it "linear pace", and From a technical point of view, this is also the easiest way to achieve animation, but it is also a completely unreal effect.
Consider a scenario where a car travels within a certain distance, it does not start at 60mph, and then suddenly reaches 0mph when it reaches the end. One is because it requires infinite acceleration (even the best car will not run from 0 to 60 in 0 seconds), otherwise it will kill all passengers. In reality, it will slowly accelerate to full speed, then when it approaches the end, it will slowly slow down until it finally stops.
What about an object dropped on the ground? It will first stop in the air, then accelerate until it reaches the ground, and then suddenly stop (then due to the accumulated kinetic energy conversion accompanied by a loud noise, bang!).
Any object in real life will accelerate or decelerate in motion. So how do we achieve this acceleration in animation? One method is to use the physics engine to model the friction and momentum of moving objects, but this will make the calculation too complicated. We call this type of equation a buffer function. Fortunately, Core Animation embeds a series of standard functions for us to use.
## CAMediaTimingFunction
So how do you use the buffer equation? First, you need to set the timingFunction property of CAAnimation, which is an object of CAMediaTimingFunction class. If you want to change the timing function of implicit animation, you can also use CATransaction's + setAnimationTimingFunction: method.
Here are some ways to create CAMediaTimingFunction, the simplest way is to call the + timingFunctionWithName: constructor. Here one of the following constants is passed in:
The kCAMediaTimingFunctionLinear option creates a linear timing function, which is also the default function when the timingFunction property of CAAnimation is empty. Linear pacing makes sense for scenes that immediately accelerate and reach the end at a constant speed (for example, a bullet fired from a gun barrel), but it looks strange by default, because it is rarely used for most animations.
The kCAMediaTimingFunctionEaseIn constant creates a method that slowly accelerates and then suddenly stops. It is suitable for the previously mentioned example of free fall, or for example the launch of a missile aimed at a target.
kCAMediaTimingFunctionEaseOut is just the opposite. It starts at a full speed and then slowly decelerates to stop. It has a weakening effect, and the applied scene such as a door closing slowly instead of slamming.
kCAMediaTimingFunctionEaseInEaseOut creates a process of slowly accelerating and then slowly decelerating. This is the way most objects move in the real world and the best choice for most animations. If only one buffer function can be used, it must be it. Then you will wonder why this is not the default choice. In fact, when using the UIView animation method, it is indeed the default, but when creating CAAnimation, you need to set it manually.
Finally, there is a kCAMediaTimingFunctionDefault, which is similar to kCAMediaTimingFunctionEaseInEaseOut, but the acceleration and deceleration processes are slightly slower. The difference between it and kCAMediaTimingFunctionEaseInEaseOut is difficult to detect. It may be that Apple thinks it is more suitable for implicit animation (then UIKit changed the idea and used kCAMediaTimingFunctionEaseInEaseOut as the default effect). Still remember that when creating explicit CAAnimation it is not the default option (in other words, the default layer behavior animation uses kCAMediaTimingFunctionDefault as their timing method).
You can experiment with a simple test project (Listing 10.1), change the code of the buffer function before running, and then click anywhere to observe how the layer moves through the specified buffer.
UIKit animations also support the use of these buffering methods, although the syntax and constants are somewhat different. To change the buffering options of UIView animations, add one of the following constants to the options parameter:
They are closely related to CAMediaTimingFunction, UIViewAnimationOptionCurveEaseInOut is the default value (there is no corresponding value for kCAMediaTimingFunctionDefault).
The specific use method is shown in Listing 10.2 (note that the additional layers added by UIView are not used here because UIKit animation does not support such layers).
Listing 10.2 Buffer test project using UIKit animation
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
// perform the animation
[UIView animateWithDuration: 1.0 delay: 0.0
options: UIViewAnimationOptionCurveEaseOut
animations: ^ {
// set the position
self.colorView.center = [[touches anyObject] locationInView: self.view];
}
completion: NULL];
}
@end
View Code buffering and key frame animation
Perhaps you will recall that the keyframe animation of color switching in Chapter 8 looks strange due to the linear transformation (see Listing 8.5), making the color transformation very unnatural. To correct this, let's use a more appropriate buffering method, such as kCAMediaTimingFunctionEaseIn, to add a little pulse effect to the color change of the layer, making it more like a colored light bulb in reality.
We don't want to apply this effect to the entire animation process. We want to repeat this buffering for each animation process, so every color change will have a pulse effect.
CAKeyframeAnimation has a timingFunctions property of type NSArray, which we can use to specify different timing functions for each animation step. But the number of specified functions must be equal to the number of elements in the keyframes array minus one, because it is a function that describes the speed of animation between each frame.
In this example, we want to use the same buffer function from beginning to end, but we also need an array of functions to tell the animation to repeat each step, instead of only buffering once in the entire animation sequence, we simply use the included An array of the same function copy is enough (see Listing 10.3).
Running the updated code, you
You will find the animation looks more natural.
Listing 10.3 Using CAMediaTimingFunction for CAKeyframeAnimation
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.