IOS Programming Controlling animation, iosanimations
IOS Programming Controlling Animation
The word "animation" is derived from a Latin word that means "the act of bringing to life. "Animations are what bring your applications to life, and when used appropriately, they can guide your users through a course of actions, orient them, and overall create a delightful experience.
Animation is derived from the Latin word that refers to the action that brings in the Declaration. Animations brings your application to life. When you use it properly, they can guide your users to create a delightful experience through a series of actions.
You will use a variety of animation techniques to animate varous views in the HypnoNerd application.
In this chapter, you will use a variety of animation techniques to animate various views applications in HypnoNerd.
1. Basic Animation
Animations are a great way to add an extra layer of polish to any application; games are not the only type of application to benefit from animations.
Animations is a good way to add additional layers to any application. Games is not the only application that makes profits from animations.
Animations can smoothly bring interface elements on screen or into focus, they can draw the user's attention to an actionable item, and they give clear indications of how your app is responding to the user's actions.
Animation can smoothly bring interface elements to the screen or attract attention, they can attract users to notice the items of their actions, they gave clear hints on how to make your app respond to users' actions.
The first type of animation you are going to use is the basic animation. A basic animation animates between a start value and an end value
The first animation you will use is the basic animation. basic animation animates action between a starting value and an end value.
The first animation you will add will animate the alpha value of the labels when they are added to the view.
The first animation you add is the animate the alpha value of the labels when they are added to the view.
MessageLabel. alpha = 0;
[UIView animateWithDuration: 0.5 animations: ^ {
MessageLabel. alpha = 1;
}];
After you enter some text and tap the return key, the labels shold fade into view. Animations provide a less jarring user experience than having the views just pop into existence.
Animations provides a better user experience than simply giving the view a better wait time.
The method animateWithDuration: animations: returns immediately. That is, it starts the animation, but does not wait around for the animation to complete.
AnimateWithDuration: directly returns the animation. That is to say, it starts an animation, but it does not wait until the animation is completed.
The simplest block-based animation method on UIView is animateWithDuration: animations:. This method takes in the duration that the animation shocould run for and a block of changes to animate.
The simplest block-based animation method in UIView is animateWithDuration: animations. This method is used to pass in the time when an animation should run and a block of changes to animate.
The animation will follow an increment-in/increment-out animation curve, which will cause the animation to begin slowly, accelerate through the middle, and finally slow down at the end.
The animation follows the animation-in/animation-out animation curve, which causes the animation to start slowly, accelerate in the middle, and finally slow down.
1.1 Timing functions
The acceleration of the animation is controlled by its timing function. The method animateWithDuration: animations: uses an expiration-in/expiration-out timing function.
The acceleration of an animation is controlled by its timing function. This animateWithDuration: animations uses the random-in/random-out timing function.
To use a driving analogy, this wocould mean the driver accelerates smoothly from rest to a constant speed, and then gradually slows down at the end, coming to rest.
In a car's analogy, this means that from rest to a constant speed and smooth acceleration, then gradually slow down until rest.
Other timing functions include linear (a constant speed from beginning to end), response-in (accelerating to a constant speed, and then ending abruptly ), and begin-out (beginning at full speed, and then slowing down at the end ).
Other timing funcitons include linear (from the start to the end are uniform), slow-in (accelerate to a constant, and then suddenly end), round-out (start at full speed, and finally slow down)
In order to use one of these other timing functions, you will need to use the UIView animation method that allows options to be specified: animateWithDuration: delay: options: animations: completion :.
To use these other timing functions, which of the following options are allowed to specify the animateWithDuration: delay: options: animations: completion :.
.
This method gives you the most control over the animation. in addition to the duration and animation block, you can also specify how long to delay before the animations shoshould begin, some options (which we will look at shortly ), and a completion block that will get called when the animation sequence completes.
This method gives you more control over the entire animation. in addition to the duration and animation blocks, you can specify how long the delay takes before the animation starts, and call some options and a completion block when the animation sequence completes.
[UIView animateWithDuration: 2 delay: 5 options: UIViewAnimationOptionCurveEaseIn animations: ^ {
MessageLabel. alpha = 1;
} Completion: NULL];
Now, as opposed to using the default queue-in/queue-out animation curve, the animation will just keep-in.
The options argument is a bitmask, so you can bitwise-or multiple values together. Here are some of the useful options that you can supply:
The optinons parameter is bit mask, so you can bitwise OR multiple values together. Here is some useful information you can provide.
These control the acceleration of the animation. Possible values are
UIViewAnimationOptionCurveEaseInOut
UIViewAnimationOptionCurveEaseIn
UIViewAnimationOptionCurveEaseOut
UIViewAnimationOptionCurveLinear
UIViewAnimationOptionAllowUserInteraction
By default, views cannot be interacted with when animating. Specifying this option will override the default. This can be useful for repeating animations, such as a pulsing view.
By default, views cannot communicate with each other when it is an animation. Specify this option to override this default. This is useful in repeating animations, such as pulsing view.
UIViewAnimationOptionRepeat
This will repeat the animation indefinitely. This is often seconds red with the UIViewAnimationOptionAutoreverse option.
This will be an indefinite animation. It is often used with UIViewAnimationOptionAutoreverse.
UIViewAnimationOptionAutoreverse
This will run the animation forward and then backward, returning the view to its initial state.
This will run animation forward and backward, and return the view to her initial state.
2 Keyframe Animations
The animations you have added so far have been basic animations; they animate from one value to another value.
Currently, all animations you have added are basic animations.
If you want to animate a view's properties through more than two values, you use a keyframe animation. A keyframe animation can be made up of any number of individual keyframes (Figure 27.3 ). you can think of keyframe animations as multiple basic animations going back to back.
If you want to animate the properties of a view to pass through more than two values, you must use keyframe animation. A keyframe animation can be composed of any number of independent keyframes. You can think of the keyframe animations as multiple basic animations.
Keyframe animations are set up similarly to basic animations, but each keyframe is added separately.
The keyframe animations and basic animations settings are very similar, but each keyframe is added separately.
To create a keyframe animation, use the parameter: delay: options: animations: completion: class method on UIView, and add keyframes in the animation block using the addKeyframeWithRelativeStartTime: relativeDuration: animations: class method.
In BNRHypnosisViewController. m, update drawHypnoticMessage: to animate the center of the labels first to the middle of the screen and then to another random position on the screen.
Update the drawHypotic Message to animate the center of the labels first in the middle of the screen, and then to any other screen position.
Keyframe animations are created using animateKeyframesWithDuration: delay: options: animations: completion :. the parameters are all the same as with the basic animation parameter t that the options are of type UIViewKeyframeAnimationOptions instead of UIViewAnimationOptions. the duration passed into this method is the duration of the entire animation.
Keyframe animations is created using animateKeyframesWithDuration: delay: options: animations: completion. The parameters are the same except that the options type is UIViewKeyframeAnimationOptions rather than UIViewAnimationOptions. The duration passed to this method is the duration of the entire animation.
Individual keyframes are added using addKeyframeWithRelativeStartTime: relativeDuration: animations :.
Separate keyframes are added by addKeyframeWithRelativeStartTime: relativeDuration: animations.
The first argument is the relative start time, which will be a value between 0 and 1. the second argument is the relative duration, which is a percent of the total duration and will also be a value between 0 and 1.
The first parameter is the relative start time, which will be the number between 0 and 1. The second parameter is relative duration. Is the overall duration percentage, and the value is still between 0 and 1.
[UIView animateKeyframesWithDuration: 5.0 delay: 0 options: 0 animations: ^ {
[UIView addKeyframeWithRelativeStartTime: 0 relativeDuration: 0.6 animations: ^ {
MessageLabel. center = self. view. center;
}];
[UIView addKeyframeWithRelativeStartTime: 0.6 relativeDuration: 0.4 animations: ^ {
Int x = arc4random () % width;
Int y = arc4random () % height;
MessageLabel. center = CGPointMake (x, y );
}];
} Completion: NULL];
3 Animation Completion
It can often be useful to know when an animation completes.
It is useful to know when animation is completed.
For instance, you might want to chain different kinds of animations together or update another object when the animation completes. To know when the animation finishes, pass a block for the completion argument.
For example, you may want to connect different types of animations or update another object when the animation is complete. To know when animation is completed, pass a block as the compleiton parameter.
Build and run the application, and log messages will appear in the console as soon as the animations complete.
You might be wondering, "What if the animation repeats? Will the completion block be executed after each repeat? "No, the completion block will only be executed once, at the very end.
You may wonder, "What if the animation is repeated? Is the completion block repeatedly executed? ", No, the completion block will be executed only once at the end.
4 Spring Animations
IOS has a powerful physics engine built into the SDK, and one of the easiest ways to use it is with the new spring animations.
IOS has a powerful physical engine built in the SDK. The simplest way to use it is to use the new spring animations.
This type of animation has a timing function like that of an actual spring. you will use this to animate the text field dropping in from the top of the screen, as if it was attached to a spring.
This type of animation has a timing functions like an actual spring. You can use this animate text field
The top slide seems to be attached to a spring.
In BNRHypnosisViewController. m, add a property for the text field to the class extension and update loadView to store the reference to the text field. Then start with the text field offscreen:
@ Property (nonatomic, weak) UITextField * textField;
Self. textField = textField;
It will be best to begin the animation as soon as the view is on the screen, so the animation code will go into viewDidAppear :. currently there is no property pointing to the text field, but you will need one in order to update its frame in viewDidAppear :.
Now, in BNRHypnosisViewController. m, override viewDidAppear: to drop in the text field using a spring animation.
-(Void) viewDidAppear :( BOOL) animated {
[Super viewDidAppear: animated];
[UIView animateWithDuration: 2.0 delay: 0.0
UsingSpringWithDamping: 0.25 initialSpringVelocity: 0.0
Options: 0 animations: ^ {
CGRect frame = CGRectMake (40, 70,240, 30 );
Self. textField. frame = frame ;}
Completion: NULL];
}
The individual components of this method are relatively straightforward:
The independent composition of this method is quite direct:
Duration: The total time the animation shocould last.
Animation will last for the whole time.
Delay
How long until the animation shoshould begin.
When to start animation.
Spring dumping:
A number between 0 and 1. The closer to 0, the more the animation oscillates.
A number between 0 and 1. The closer it is to 0, the more stable the animation is.
Spring velocity The relative velocity of the view when the animation is to begin. You will almost always pass in 0 for this.
The speed of the related view when the animation starts. You always pass 0 to this.
Options
UIViewAnimationOptions, just like with the other animations.
UIViewAnimationOptions is like other animations.
Animaitons
A block of changes to animate on one or more views.
Changed the code block of animate on one or multiple views.
Completion
A block to run when the animation is finished.
The code block that runs when animation is complete.