animation(动画)
动画是用户界面中一个重要的部分,当我们使用core animation时,动画是完全自动的,不需要动画循环或者定时器,我们的程序不需要一帧一帧的去绘制,也不需要去跟踪动画当前的状态。动画发生在一个独立的线程里,不需要与应用程序交互。
本篇文章就主要讲述一下动画类,以及描述一下如何创建明确的(explicit)动画和隐含的(implicit)动画。
core Animation提供了一个animation类的集合可以供我们应用程序使用。
CABasicAnimation提供简单的layer属性值之间的interpolation。
CAKeyframeAnimation提供了关键帧动画的支持。我们可以指定layer属性进行动画的关键路径,一个包含每一个阶段动画的数组。
CATransition提供一个过度效果(影响到整个layer的内容),当动画时,它fade(淡化),push(推),或者reveals(揭露)layer的内容。此动画可以用自己提供的自定义 core image filters去扩展。
CAAnimationGroup允许将一组动画放在一个数组里面,然后同时执行。
除了指定具体的动画类型,我们必须要指定动画的持续时间, pacing等。动画可以重复,设置重复的次数,或者当动画周期完成时是否自动反转。animation类和CAMediaTiming协议提供了这里所有的功能。
core animation的隐含动画假定所有改变的可动画的layer属性应该是渐进和异步的。改变一个可动画的layer属性值可以隐含的引起一个动画使属性的值从旧值变为新值。
隐含动画使用该属性使用的默认的动画中指定的时间。可以覆盖此默认的时间,在6中我们在介绍。
当想创建一个动画让其持续运行时,显式的动画是很有用的。
可以通过发送消息addAnimation:forKey:给layer来启动显式动画,传递动画类和标识作为参数。
一旦加入,显式动画就会开始运行直到其动画完成,或者从layer上remove。标识就是用来停止一个动画的,removeAnimationForKey:,也可以通过removeAllAnimations停止一个layer所有的动画。
1.animation类和timing(时序)
2.implicit animation
下面一句话就是简单的触发一个隐含的动画时layer从当前的position到新的position。
// assume that the layer is current positioned at (100.0,100.0) |
theLayer.position=CGPointMake(500.0,500.0); |
也可以一次改变多个layer属性,或者同时改变多个layer的属性值都可以。下面代码说明了此。
// animate theLayer's opacity to 0 while moving it |
// further away in the layer |
theLayer.opacity=0.0; |
theLayer.zPosition=-100; |
|
// animate anotherLayer's opacity to 1 |
// while moving it closer in the layer |
anotherLayer.opacity=1.0; |
anotherLayer.zPosition=100.0; |
3.明确的动画
明确的动画需要我们去创建一个animation对象,设置开始和结束值。一个明确的动画不会自动开始除非你将其应用到layer中。下面的代码将创建一个明确的动画,在3秒中过度一个layer的opacity从opaque到full transparent(全部透明),并且在返回原样。直到将其加到layer上时,动画才开始。
CABasicAnimation *theAnimation; |
|
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; |
theAnimation.duration=3.0; |
theAnimation.repeatCount=2; |
theAnimation.autoreverses=YES; |
theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; |
theAnimation.toValue=[NSNumber numberWithFloat:0.0]; |
[theLayer addAnimation:theAnimation forKey:@"animateOpacity"]; |
// The selection layer will pulse continuously. |
// This is accomplished by setting a bloom filter on the layer |
|
// create the filter and set its default values |
CIFilter *filter = [CIFilter filterWithName:@"CIBloom"]; |
[filter setDefaults]; |
[filter setValue:[NSNumber numberWithFloat:5.0] forKey:@"inputRadius"]; |
|
// name the filter so we can use the keypath to animate the inputIntensity |
// attribute of the filter |
[filter setName:@"pulseFilter"]; |
|
// set the filter to the selection layer's filters |
[selectionLayer setFilters:[NSArray arrayWithObject:filter]]; |
|
// create the animation that will handle the pulsing. |
CABasicAnimation* pulseAnimation = [CABasicAnimation animation]; |
|
// the attribute we want to animate is the inputIntensity |
// of the pulseFilter |
pulseAnimation.keyPath = @"filters.pulseFilter.inputIntensity"; |
|
// we want it to animate from the value 0 to 1 |
pulseAnimation.fromValue = [NSNumber numberWithFloat: 0.0]; |
pulseAnimation.toValue = [NSNumber numberWithFloat: 1.5]; |
|
// over a one second duration, and run an infinite |
// number of times |
pulseAnimation.duration = 1.0; |
pulseAnimation.repeatCount = HUGE_VALF; |
|
// we want it to fade on, and fade off, so it needs to |
// automatically autoreverse.. this causes the intensity |
// input to go from 0 to 1 to 0 |
pulseAnimation.autoreverses = YES; |
|
// use a timing curve of easy in, easy out.. |
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEa |
|
// add the animation to the selection layer. This causes |
// it to begin animating. We'll use pulseAnimation as the |
// animation key name |
[selectionLayer addAnimation:pulseAnimation forKey:@"pulseAnimation"]; |
(上面的代码用到了过滤器)
4.启动和停止显式动画