Core Animation
Core Animation, the Chinese translation is a central animation, it is a very powerful set of animation processing API, using it can make very beautiful animation effect, and often do more with less. In other words, very powerful functionality can be achieved with a small amount of code.
Core animation can be used on Mac OS X and iOS platforms.
The animation execution of Core animation is performed in the background and does not block the main thread.
It is important to note that the Core animation is directly acting on the Calayer, not uiview.
At the 2007 WWDC Congress, amid the power of core animation
如果是xcode5之前的版本,使用它需要先添加QuartzCore.framework和引入对应的框架<QuartzCore/QuartzCore.h>开发步骤:1.使用它需要先添加QuartzCore.framework框架和引入主头文件<QuartzCore/QuartzCore.h>2.初始化一个CAAnimation对象,并设置一些动画相关属性3.通过调用CALayer的addAnimation:forKey:方法增加CAAnimation对象到CALayer中,这样就能开始执行动画了4.通过调用CALayer的removeAnimationForKey:方法可以停止CALayer中的动画
Caanimation Inheritance Structure
Note: The black dashed line in the figure represents "inheriting" a class, and the red dashed line represents "abide" by a protocol
Caanimation Introduction
Is the parent of all animated objects and is responsible for controlling the duration and speed of the animation, an abstract class that cannot be used directly, and should use its specific subclasses
Property Description: (Red represents a property from the Camediatiming protocol)
Duration: Duration of animation
RepeatCount: Repeat number, infinite loop can be set huge_valf or maxfloat
RepeatDuration: Repeat Time
Removedoncompletion: The default is yes, which is removed from the layer after the animation has finished executing, and the drawing reverts to the state before the animation was executed. If you want the layer to remain displayed after the animation has been executed, set it to no, but also set Fillmode to Kcafillmodeforwards
Fillmode: Determines the behavior of the current object during a non-active time period. Like before the animation starts or after the animation ends
BeginTime: Can be used to set the animation delay execution time, if you want to delay 2s, set to Cacurrentmediatime () +2,cacurrentmediatime () is the current time of the layer
Timingfunction: Speed control function to control the rhythm of animation operation
Delegate: Animation agent
Caanimation Animation fill Mode
Fillmode Property value (if you want Fillmode to be valid, it is best to set removedoncompletion = NO)
Kcafillmoderemoved This is the default value, that is, when the animation starts and after the end of the animation, the animation has no effect on the layer, the end of the animation, the layer will revert to the previous state
Kcafillmodeforwards when the animation is finished, the layer will remain the last state of the animation
Kcafillmodebackwards before the animation begins, just add the animation to a layer,layer and immediately enter the initial state of the animation and wait for the animation to begin.
Kcafillmodeboth This is actually the synthesis of the above two. The layer is in the initial state of the animation before the animation is added, and the layer retains the last state of the animation after the animation is finished.
Caanimation speed control function
速度控制函数(CAMediaTimingFunction) kCAMediaTimingFunctionLinear(线性)://匀速,给你一个相对静态的感觉kCAMediaTimingFunctionEaseIn(渐进)://动画缓慢进入,然后加速离开kCAMediaTimingFunctionEaseOut(渐出)://动画全速进入,然后减速的到达目的地kCAMediaTimingFunctionEaseInEaseOut(渐进渐出)://动画缓慢的进入,中间加速,然后减速的到达目的地。这个是默认的动画行为。
Caanimation Animation Agent Method
CAAnimation在分类中定义了代理方法@interface NSObject (CAAnimationDelegate)/* Called when the animation begins its active duration. */- (void)animationDidStart:(CAAnimation *)anim;- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;@end
Pause and resume animations on Calayer
-(void ) PauseLayer: ( calayer*) layer{cftimeinterval pausedtime = [Layer Converttime:cacurrentmediatime () Fromlayer:nil ]; //let Calayer time stop walking layer.speed = 0.0 ; //let Calayer time stay at Pausedtime this moment layer.timeoffset = pausedtime;}
#pragma mark 恢复CALayer的动画-(void)resumeLayer:(CALayer*)layer{ CFTimeInterval pausedTime = layer.timeOffset; // 1. 让CALayer的时间继续行走 1.0; // 2. 取消上次记录的停留时刻 0.0; // 3. 取消上次设置的时间 0.0; // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime) CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause) layer.beginTime = timeSincePause;}
Capropertyanimation
is a subclass of Caanimation and an abstract class, to create an animated object, you should use its two subclasses:
Cabasicanimation
Cakeyframeanimation
Property Description:
KeyPath: By specifying a property name of Calayer as KeyPath (nsstring type), and modifying the value of this property of Calayer to achieve the corresponding animation effect. For example, if you specify @ "position" as KeyPath, the value of the position property of the Calayer is modified to achieve the animated effect of panning
Cabasicanimation Basic Animations
Basic animation, a subclass of Capropertyanimation
Property Description:
Fromvalue:keypath the initial value of the corresponding property
Tovalue:keypath the end value of the corresponding property
Animation Process Description:
As the animation progresses, the value of the keypath corresponding property is gradually changed from Fromvalue to Tovalue during the duration of the duration length.
KeyPath content is an animated animatable property of Calayer
If Fillmode=kcafillmodeforwards is removedoncomletion=no at the same time, after the animation finishes, the layer remains displayed after the animation is executed. But in essence, the property value of the layer is still the initial value before the animation is executed, and it is not really changed.
Cakeyframeanimation Key-Frame animations
Keyframe animation, also capropertyanimation subclass, differs from Cabasicanimation:
Cabasicanimation can only change from one numeric value (Fromvalue) to another (Tovalue), and Cakeyframeanimation saves the values with a Nsarray
Property Description:
Values: The Nsarray object above. The elements inside are called "keyframes" (keyframe). The animated object displays each keyframe in the values array in the specified time (duration), in turn
Path: You can set a cgpathref, Cgmutablepathref, and let the layer move along the path trajectory. Path only works on Calayer's anchorpoint and position. If path is set, then values will be ignored
Keytimes: You can specify a corresponding point in time for the corresponding keyframe, with a value range of 0 to 1.0,keytimes each time value corresponds to each frame in values. If Keytimes is not set, the time of each keyframe is equally divided
Cabasicanimation can be seen as a cakeyframeanimation with only 2 keyframes
Caanimationgroup Animation Group
Animation group, which is a subclass of Caanimation, can save a set of animated objects, and after the Caanimationgroup object is added to the layer, all animated objects in the group can run concurrently simultaneously
Property Description:
Animations: A nsarray used to hold a set of animated objects
By default, a set of animated objects runs concurrently, or you can change the start time of an animation by setting the BeginTime property of the animated object
Catransition transition Animations
Catransition is a subclass of caanimation that is used to animate transitions and animate layers to move out of the screen and into the screen. iOS has less transition animations than Mac OS X
Uinavigationcontroller is the animation effect of pushing the controller's view into the screen via catransition
Animation properties:
Type: Animation transition types
Subtype: Animation transition Direction
Startprogress: Start of Animation (percentage in overall animation)
Endprogress: End of Animation (percentage in overall animation)
Transitions Animation Transition Effect
Using the UIView animation function to implement the Transition animation single view
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;参数说明://duration:动画的持续时间//view:需要进行转场动画的视图//options:转场动画的类型//animations:将改变视图属性的代码放在这个block中//completion:动画结束后,会自动调用这个block
Using the UIView animation function to achieve a two-view transition animation
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;参数说明://duration:动画的持续时间//options:转场动画的类型//animations:将改变视图属性的代码放在这个block中//completion:动画结束后,会自动调用这个block
Cadisplaylink
Cadisplaylink is a clock mechanism triggered by the screen refresh frequency, which executes approximately 60 times per second
Cadisplaylink is a timer that keeps the drawing code in sync with the refresh rate of the view, while Nstimer does not ensure the exact time at which the timer is actually triggered
How to use:
Defining Cadisplaylink and developing trigger invocation methods
Add a display link to the main run loop queue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development-core Animation animation