从大神代码中学习CAAnimation

 首先申明,以下项目代码摘自ShareOfCoreAnimation项目中,本人只是将代码功能分类总结,方便自己和他人学习使用。
 如有问题,欢迎指正,联系QQ:1136523628

代码版权属作者所有:
这里写图片描述

一,动画结构:


  1. NSObject
  2. CAAnimation : NSObject < NSCoding, NSCopying, CAMediaTiming, CAAction>
    抽象类,包含CALayer相关的数据模型,和动画开始,结束的代理方法,时间函数
  3. 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
  4. CABasicAnimation : CAPropertyAnimation
    经常使用的动画,通过fromValue,toValue,bgValue直接操作CALayer的属性z值。
  5. CAKeyframeAnimation : CAPropertyAnimation
    关键帧动画,保存关键帧,路径,时间等:
       NSArray *values,
      CGPathRef path,
      NSArray *keyTimes,
      NSArray *timingFunctions,
      NSString *calculationMode,
      NSArray *tensionValues,
      NSArray *continuityValues,
      NSArray *biasValues,
      NSString *rotationMode
  6. CATransition : CAAnimation
    动画事务类,fade, moveIn, push reveal的动画
  7. 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值