从大神代码中学习CAAnimation
2015-09-08 17:26
183 查看
首先申明,以下项目代码摘自ShareOfCoreAnimation项目中,本人只是将代码功能分类总结,方便自己和他人学习使用。 如有问题,欢迎指正,联系QQ:1136523628
代码版权属作者所有:
一,动画结构:
NSObject
CAAnimation : NSObject < NSCoding, NSCopying, CAMediaTiming, CAAction>
抽象类,包含CALayer相关的数据模型,和动画开始,结束的代理方法,时间函数
CAPropertyAnimation : CAAnimation
属性动画,抽象类,通过keyPath操作CALayer 的属性,包含additive和cumulative属性
transform.scale = 比例轉換
transform.scale.x = 闊的比例轉換
transform.scale.y = 高的比例轉換
transform.rotation.z = 平面圖的旋轉
opacity = 透明度
margin
zPosition
backgroundColor 背景颜色
cornerRadius 圆角
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
CABasicAnimation : CAPropertyAnimation
经常使用的动画,通过fromValue,toValue,bgValue直接操作CALayer的属性z值。
CAKeyframeAnimation : CAPropertyAnimation
关键帧动画,保存关键帧,路径,时间等:
NSArray *values,
CGPathRef path,
NSArray *keyTimes,
NSArray *timingFunctions,
NSString *calculationMode,
NSArray *tensionValues,
NSArray *continuityValues,
NSArray *biasValues,
NSString *rotationMode
CATransition : CAAnimation
动画事务类,fade, moveIn, push reveal的动画
CAAnimationGroup : CAAnimation
动画组
1.基本动画
- (IBAction)caRotateAction:(id)sender { CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; rotation.fromValue = @(0); rotation.toValue = @(M_PI*2); rotation.duration = 1.f; rotation.repeatCount = /*INFINITY*/ 1; rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; // rotation.fillMode = kCAFillModeForwards;//保持动画末状态 // rotation.removedOnCompletion = NO; self.demoImgView.transform = CGAffineTransformMakeRotation(M_PI*2); [self.demoImgView.layer addAnimation:rotation forKey:@"an_roate"]; }
- (IBAction)YRotateAction:(id)sender { CABasicAnimation *TransformAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; TransformAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; TransformAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 1, 0)]; TransformAnim.cumulative = YES; TransformAnim.duration = 3; TransformAnim.repeatCount = 2; [self.demoImgView.layer addAnimation:TransformAnim forKey:nil]; }
2 . 关键帧动画
震动效果 - (IBAction)shakeAnimation:(id)sender { CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; animation.keyPath = @"position.x"; animation.values = @[ @0, @10, @-10, @10, @0 ]; animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ]; animation.duration = 0.4; animation.additive = YES; [self.tracker.layer addAnimation:animation forKey:@"shake"]; }
- (IBAction)trackAnimation:(id)sender { CGRect boundingRect = CGRectMake(-100, -185, 200, 200); CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation]; orbit.keyPath = @"position"; orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(boundingRect, NULL)); orbit.duration = 4; orbit.additive = YES; orbit.repeatCount = HUGE_VALF; orbit.calculationMode = kCAAnimationPaced; orbit.rotationMode = /*kCAAnimationRotateAuto*/ nil; [self.tracker.layer addAnimation:orbit forKey:@"ani-track"]; }
3 . 塞贝尔曲线
1. (void)setupAnimationLayer { [self.animationLayer removeFromSuperlayer]; self.animationLayer = nil; CGPoint bottomLeft = CGPointMake(35.f, 400.f); CGPoint topLeft = CGPointMake(35.f, 200.f); CGPoint bottomRight = CGPointMake(285.f, 400.f); CGPoint topRight = CGPointMake(285.f, 200.f); CGPoint roofTip = CGPointMake(160.f, 100.f); UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:bottomLeft]; [path addLineToPoint:topLeft]; [path addLineToPoint:roofTip]; [path addLineToPoint:topRight]; [path addLineToPoint:topLeft]; [path addLineToPoint:bottomRight]; [path addLineToPoint:topRight]; [path addLineToPoint:bottomLeft]; [path addLineToPoint:bottomRight]; CAShapeLayer *pathLayer = [CAShapeLayer layer]; pathLayer.frame = CGRectMake(35, 100, 250, 200); pathLayer.bounds = CGRectMake(35, 100, 250, 200); pathLayer.path = path.CGPath; pathLayer.strokeColor = [[UIColor blackColor] CGColor]; pathLayer.fillColor = nil; pathLayer.lineWidth = 6.f; pathLayer.lineJoin = kCALineJoinRound; [self.view.layer addSublayer:pathLayer]; [self setAnimationLayer:pathLayer]; } 2. (IBAction)startAnimation:(id)sender { [self setupAnimationLayer]; [self.animationLayer removeAllAnimations]; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; pathAnimation.duration = 10.0; pathAnimation.fromValue = @(0); pathAnimation.toValue = @(1); [self.animationLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; }
4 .CAShapeLayer
- (IBAction)shapeLayerDisplay:(id)sender { UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(70, 70) radius:50.f startAngle:0 endAngle:2*M_PI clockwise:YES]; CAShapeLayer *shape = [CAShapeLayer layer]; shape.path = path.CGPath; self.demoImgView.layer.mask = shape; }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Gifski:一个跨平台的高质量 GIF 编码器
- 模仿动画的放大缩小容器
- jQuery实现美观的多级动画效果菜单代码
- php判断GIF图片是否为动画的方法
- jQuery实现动画效果circle实例
- 浅析JavaScript动画
- js排序动画模拟-插入排序
- javascript+HTML5的Canvas实现Lab单车动画效果
- js实现按钮颜色渐变动画效果
- jQuery实现带有洗牌效果的动画分页实例
- jquery实现先淡出再折叠收起的动画效果
- jQuery使用animate创建动画用法实例
- jquery带动画效果幻灯片特效代码