iOS PRogramming Controlling Animations 動(dòng)畫
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 源自拉丁詞意思是帶入聲明的動(dòng)作。animations 是把你的應(yīng)用帶到life,當(dāng)你恰當(dāng)?shù)氖褂茫麄兡芤龑?dǎo)你的用戶通過一系列動(dòng)作,orient 他們,全部創(chuàng)建一個(gè)delightful experience.
you will use a variety of animation techniques to animate various views in the HypnoNerd application.
在本章,你將使用各種各樣的animation techniques to animate 各種views在HypnoNerd應(yīng)用。
1. Basic Animations 基礎(chǔ)的動(dòng)畫
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 是一種很好的方式添加到任何應(yīng)用上的額外的層。games 不是從animations 獲利的唯一一種應(yīng)用。
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.
動(dòng)畫能夠平滑的把界面元素帶到屏幕上或者引起注意,他們能吸引用戶的注意到他們動(dòng)作的item,他們給了清晰的暗示來怎樣讓你的app響應(yīng)用戶的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
你將使用的第一個(gè)動(dòng)畫是basic animation.basic animation animates 在一個(gè)開始的值和一個(gè)end value之間動(dòng)作。
The first animation you will add will animate the alpha value of the labels when they are added to the view.
你將添加的第一個(gè)動(dòng)畫是animate the alpha value of the labels 當(dāng)他們添加到view上時(shí)。
messageLabel.alpha=0;
? ? ? ? [UIView animateWithDuration:0.5 animations:^{
? ? ? ? ? ? messageLabel.alpha=1;
? ? ? ? }];
?
After you enter some text and tap the return key, the labels should fade into view. Animations provide a less jarring user experience than having the views just pop into existence.
Animations 提供了一個(gè)更少的用戶等待時(shí)間比直接讓view 出現(xiàn)體驗(yàn)更好。
The method animateWithDuration:animations: returns immediately. That is, it starts the animation, but does not wait around for the animation to complete.
animateWithDuration:animations直接返回。也就是說它開始一個(gè)animation,但是并不會(huì)等到動(dòng)畫完成。
The simplest block-based animation method on UIView is animateWithDuration:animations:. This method takes in the duration that the animation should run for and a block of changes to animate.?
最簡(jiǎn)單的block—based animation 方法在UIView是animateWithDuration:animations.這個(gè)方法傳入一個(gè)動(dòng)畫應(yīng)該運(yùn)行的時(shí)間和一個(gè)block? of changes to animate.
The animation will follow an ease-in/ease-out animation curve, which will cause the animation to begin slowly, accelerate through the middle, and finally slow down at the end.
動(dòng)畫會(huì)跟隨ease-in/ease-out animation curve,這將導(dǎo)致animation 開始慢,中間加速,最后減速的效果。
1.1 Timing functions?
The acceleration of the animation is controlled by its timing function. The method animateWithDuration:animations: uses an ease-in/ease-out timing function.?
動(dòng)畫的acceleration 是由它的timing function 控制的。這個(gè)animateWithDuration:animations使用了ease-in/ease-out timing function.
To use a driving analogy, this would mean the driver accelerates smoothly from rest to a constant speed, and then gradually slows down at the end, coming to rest.
用一個(gè)開車的比喻,這意味著從rest 到一個(gè)恒速平滑的加速,然后漸漸的減速,直到rest。
Other timing functions include linear (a constant speed from beginning to end), ease-in (accelerating to a constant speed, and then ending abruptly), and ease-out (beginning at full speed, and then slowing down at the end).
其他的timing funcitons 包括linear(從開始到結(jié)束都是勻速),ease-in(加速到一個(gè)常量,然后突然結(jié)束),ease-out(開始全速,最后減速)
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:.
為了使用這些其他的timing functions,你需要使用哪些允許options 來指明的animateWithDuration:delay:options:animations:completion:.
的UIView animation 方法。
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 should begin, some options (which we will look at shortly), and a completion block that will get called when the animation sequence completes.
這個(gè)方法給你了更多的控制在整個(gè)animation.除了duration 和animation block ,你可以指明在動(dòng)畫開始之前需要delay 多久,和一些options和一個(gè)completion block 當(dāng)animation sequence completes 時(shí)被調(diào)用。
?[UIView animateWithDuration:2 delay:5 options:UIViewAnimationOptionCurveEaseIn animations:^{
? ? ? ? ? ? messageLabel.alpha=1;
? ? ? ? } completion:NULL];
Now, as opposed to using the default ease-in/ease-out animation curve, the animation will just ease- 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:
optinons 參數(shù)是bit mask,所以你可以bitwise或者multiple values together.這里是你能提供的一些有用的信息。
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.
默認(rèn)情況下,views 不能交流當(dāng)animating時(shí)。指明這個(gè)選項(xiàng)將override 這個(gè)default.這在repeating animations ,例如pulsing view 時(shí)會(huì)很有用
UIViewAnimationOptionRepeat
This will repeat the animation indefinitely. This is often paired with the UIViewAnimationOptionAutoreverse option.
這個(gè)將無限期的這個(gè)動(dòng)畫。它經(jīng)常與UIViewAnimationOptionAutoreverse一起用。
UIViewAnimationOptionAutoreverse
This will run the animation forward and then backward, returning the view to its initial state.
這將運(yùn)行animation 前進(jìn)然后后退,返回view到她的 初始狀態(tài)。
2 ?Keyframe Animations
The animations you have added so far have been basic animations; they animate from one value to another value.
你目前添加的動(dòng)畫都是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.
如果你想animate 一個(gè)view 的properties經(jīng)過多于兩個(gè)值,你要用keyframe animation. 一個(gè)keyframe animation 能夠由任意數(shù)量的獨(dú)立的keyframes 組成。你可以認(rèn)為keyframe animations 為multiple basic animations 來來回回。
Keyframe animations are set up similarly to basic animations, but each keyframe is added separately.
keyframe animations 與basic animations 的設(shè)置非常相近,但是每個(gè)keyframe被分別的添加。
?
To create a keyframe animation, use the animateKeyframesWithDuration: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.
?
更新drawHypotic Message? 來animate the? center of the labels 先在screen 的中間,然后到任意其他的屏幕的位置。
Keyframe animations are created using animateKeyframesWithDuration:delay:options:animations:completion:. The parameters are all the same as with the basic animation except 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 使用animateKeyframesWithDuration:delay:options:animations:completion來創(chuàng)建。參數(shù)除了options 的類型是UIViewKeyframeAnimationOptions而不是UIViewAnimationOptions之外,都一樣。傳遞給這個(gè)方法的duration 是整個(gè)animation 的duration。
Individual keyframes are added using addKeyframeWithRelativeStartTime:relativeDuration:animations:.
單獨(dú)的keyframes 由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.
第一個(gè)參數(shù)是相對(duì)開始時(shí)間,將會(huì)是0到1 之間的數(shù)。第二個(gè)參數(shù)是相對(duì)持續(xù)時(shí)間。是整個(gè)duration 百分比,值仍然是在0-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.?
知道animation什么時(shí)候完成是有用的。
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.
例如,你可能想連接不同種類的動(dòng)畫一起或者當(dāng)animation 完成時(shí),更新另一個(gè)對(duì)象。為了知道animation 什么時(shí)候完成,傳遞一個(gè)block 為compleiton 參數(shù)。
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.
你可能會(huì)想:"如果animation 重復(fù)怎么辦?completion block 是否被反復(fù)執(zhí)行?",不是,completion block 將僅僅執(zhí)行一次,在最后。
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 有一個(gè)強(qiáng)大的物理引擎構(gòu)建在SDK中,使用它的最簡(jiǎn)單的方式是用新的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.
這個(gè)類型的animation 有一個(gè)timing functions 像一個(gè)實(shí)際的spring。 你可以用這個(gè)animate 這個(gè)text field 從screen
頂端滑落,好像是依附在一個(gè)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:
這個(gè)方法獨(dú)立的組成是相當(dāng)?shù)闹苯樱?/span>
duration :The total time the animation should last.
animation 將持續(xù)的整個(gè)時(shí)間。
delay
How long until the animation should begin.
什么時(shí)候開始animation.
spring dumping :
A number between 0 and 1. The closer to 0, the more the animation oscillates.
在0和1之間的數(shù)字。越靠近0,動(dòng)畫越震蕩。
?spring velocity? ? The relative velocity of the view when the animation is to begin. You will almost always pass in 0 for this.
當(dāng)動(dòng)畫開始時(shí),相關(guān)view 的速度。你總是傳遞0給這個(gè)。
options
UIViewAnimationOptions, just like with the other animations.?
UIViewAnimationOptions,就像其他的animations。
animaitons
A block of changes to animate on one or more views.
改變了animate on one 或多個(gè)views 的代碼塊
completion
A block to run when the animation is finished.
當(dāng)animation 完成時(shí)運(yùn)行的代碼塊。
?
?
?
?
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注