您的位置:首页 > 编程语言

从大神代码中学习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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动画 ios