所有核心动画的动画类都是从 CAAnimation 类继承而来 。核心动画额外提供了一系列抽象的和细化的动画类,比如:
CATransition 提供了一个图层变化的过渡效果,它能影响图层的整个内容。 动画进行的时候淡入淡出(fade)、推(push)、显露(reveal)图层的内容。
CAAnimationGroup 允许一系列动画效果组合在一起,并行显示动画。
CAPropertyAnimation 是一个抽象的子类,它支持动画的显示图层的关键路径中指定的属性
CABasicAnimation 简单的为图层的属性提供修改。
1 -(void)doOpacity//改变透明度 2 { 3 //需要使用CABasicAnimation类 4 //1.layer动画流程 创建一个动画 设置相关参数 为layer添加动画 5 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 6 [animation setFromValue:[NSNumber numberWithFloat:1.0]]; 7 [animation setToValue:[NSNumber numberWithFloat:0]]; 8 [animation setDuration:2.0]; 9 [animation setRepeatDuration:20.0]; 10 // [animation setRepeatCount:5]; 11 [animation setAutoreverses:YES]; 12 [animation setDuration:2.0]; 13 [animation setRepeatDuration:20.0]; 14 //[animation setRepeatCount:5]; 15 [bigImage.layer addAnimation:animation forKey:@"abc"]; 16 }
CAKeyframeAnimation 支持关键帧动画,你可以指定的图层属性的关键路径
1 -(void)doKeyFrameAnimation 2 { 3 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 4 NSArray *values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(100, 120)],[NSValue valueWithCGPoint:CGPointMake(140, 180)],[NSValue valueWithCGPoint:CGPointMake(130, 160)],[NSValue valueWithCGPoint:CGPointMake(120, 140)],[NSValue valueWithCGPoint:CGPointMake(0,300)], nil]; 5 [animation setValues:values]; 6 7 NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.1],[NSNumber numberWithFloat:0.3], [NSNumber numberWithFloat:0.6],[NSNumber numberWithFloat:0.9], 8 [NSNumber numberWithFloat:1],nil];//完成10%,30%,60%,90%,100% 9 [animation setKeyTimes:times]; 10 [animation setDuration:10]; 11 [animation setRepeatCount:10]; 12 [bigImage.layer addAnimation:animation forKey:@"1"]; 13 }
动画,包括动画的每个阶段的价值,以及关键帧时间和计时功能的一系列值。在 动画运行是,每个值被特定的插入值替代。
Core Animation使用了setType与setSubtype组合,这使用个比较保险,因为他的参数就是官方API里定义的,他们的参数说明可以参考如下:
- setType:可以返回四种类型:
- kCATransitionFade淡出
- kCATransitionMoveIn覆盖原图
- kCATransitionPush推出
- kCATransitionReveal底部显出来
- setSubtype:也可以有四种类型:
- kCATransitionFromRight;
- kCATransitionFromLeft(默认值)
- kCATransitionFromTop;
- kCATransitionFromBottom
还有一种设置动画类型的方法,不用setSubtype,只用setType:
[animation setType:@"suckEffect"];
这里的suckEffect就是效果名称,可以用的效果主要有:
- pageCurl 向上翻一页
- pageUnCurl 向下翻一页
- rippleEffect 滴水效果
- suckEffect 收缩效果,如一块布被抽走
- cube 立方体效果
- oglFlip 上下翻转效果