原文地址:http://superman474.blog.163.com/blog/static/120661462011857653190/
animation(动画)
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.启动和停止显式动画