Core Animation(核⼼动画)是一组很是强大的动画处理API,使用它能作出很是炫丽的动画效果,并且每每是事半功倍框架
使⽤它须要先添加QuartzCore.framework和引入对应的框架动画
<QuartzCore/QuartzCore.h>url
###三、CAAnimation CAAimation是全部动画对象的基类,负责控制动画的持续时间和速度,是个抽象类,不能直接使用,应该使用它具体的子类.net
CABasicAnimation是CAPropertyAnimation的子类线程
扩展现例代码-平移:code
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:"transform.translation.x"]; animation.toValue = @320; animation.duration = 1; animation,timingFuntion = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; animation.fillMode = KCAFillModeForwards animation.removedOnCompletion = NO; [self.animationView.layer addAnimation:animation forKey:@"animation"];
扩展现例代码-缩放:orm
CABasicAnimation *animation=[CABasicAnimationanimationWithKeyPath:"transform.scale"]; animation.toValue = @2; animation.duration = 0.25; animation.repeatCount = 1; animation.autoreverses = YES; [self.animationView.layer addAnimation:animation forKey:@"animation"];
--
动画的设置对象
// 四、动画时间 animation.duration = 2.0; /* 五、动画的填充模式: kCAFillModeForwards kCAFillModeBackwards kCAFillModeBoth kCAFillModeRemoved */ animation.fillMode = kCAFillModeForwards; // 六、动画后是否移除动画后的状态(回到原始状态),默认是YES, 前提是要设置fillModle为:kCAFillModeForwards animation.removedOnCompletion = NO; // 七、是否有回复效果 animation.autoreverses = YES; // 八、设置动画重复次数 animation.repeatCount = HUGE_VALF; // HUGE_VALF 最大浮点数,表示无限次重复 // 九、播放动画的速度 animation.speed = 2;
CAKeyframeAnimation,也是CAPropertyAnimation的子类。但关键帧动画和简单动画的区别是:简单动画只能从一个数值过渡到另外一个数值,而关键帧动画有一个NSArray来保存多个数值的过渡。继承
绘制路径代码以下:ip
// 绘制路径 - (UIBezierPath *)path { // 椭圆 // UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.view.bounds]; // 圆角矩形 // UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds cornerRadius:50]; // 内切圆 // UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 100, 300, 300)]; // 贝塞尔曲线 UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0, TScreenHeight)]; CGPoint point_1 = CGPointMake(TScreenWidth, TScreenHeight); CGPoint controPoint_1 = CGPointMake(TScreenWidth / 2, - TScreenHeight); // CGPoint controPoint_2 = CGPointMake(TScreenWidth / 4 * 3, TScreenHeight); [path addQuadCurveToPoint:point_1 controlPoint:controPoint_1]; // [path addCurveToPoint:point_1 controlPoint1:controPoint_1 controlPoint2:controPoint_2]; return path; }
CATransition并不像属性动画那样平滑的在两个值之间作动画,而是影响到整个图层的变化,过渡动画首先展现以前的图层外观,而后经过一个交换过渡到新的外观
示例代码以下:
- (CAAnimation *)transitionAnimation { CATransition *transitionAni = [CATransition animation]; transitionAni.duration = 1.0; /* 1. fade 淡出效果 2. moveIn 进入效果 3. push 推出效果 4. reveal 移出效果 // 未公开的 5. cube 立方体翻转效果 6. suckEffect 抽走效果 7. rippleEffect 水波效果 8. pageCurl 翻开页效果 9. pageUnCurl 关闭页效果 10. cameraIrisHollowOpen 相机镜头打开效果 11. cameraIrisHollowClose 相机镜头关闭效果 */ transitionAni.type = kCATransitionPush; // transitionAni.type = @"push"; // 转场的方向:`fromLeft', `fromRight', `fromTop' `fromBottom' transitionAni.subtype = @"fromTop"; // 开始转场和结束转场的进度位置 // transitionAni.startProgress = 0.5; // transitionAni.endProgress = 1; return transitionAni; }
##5、动画的暂停和继续
- (void)pauseAnimation { NSLog(@"CACurrentMediaTime:%f",CACurrentMediaTime()); // CACurrentMediaTime(): 当前媒体时间,表示系统启动后到当前的秒数,当系统重启后这个时间也重置 CFTimeInterval pauseTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; // 设置动画的时间的偏移 layer.timeOffset = pauseTime; layer.speed = 0; } - (void)resumeAnimation { // 获取暂停时的时间 CFTimeInterval pasuseTime = [layer timeOffset]; layer.speed = 1; layer.timeOffset = 0; layer.beginTime = 0; // 设置开始的时间(继续动画,这样设置至关于让动画等待的秒数等于暂停的秒) layer.beginTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pasuseTime; } - (CAAnimation *)rotationAnimation { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.duration = 2.0; animation.byValue = @(2 * M_PI); animation.repeatCount = HUGE_VALF; // 设置动画开始的媒体时间(用于设置动画的延迟启动),要加上CACurrentMediaTime animation.beginTime = CACurrentMediaTime() + 2; [layer addAnimation:animation forKey:@"rotaion"];
<h3>附:CALayer中能够用来作动画的属性 </h3> 