CABasicAnimation绘制缩放核心动画效果

CABasicAnimation类的使用方式就是基本的关键帧动画。
所谓关键帧动画,就是将Layer的属性作为KeyPath来注册,指定动画的起始帧和结束帧,然后自动计算和实现中间的过渡动画的一种动画方式。

    // 设定轨迹的起始点和转折点 
    UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
    [bezierPath moveToPoint:CGPointMake(kScreenWidth, 200)];
    [bezierPath addCurveToPoint:CGPointMake(0, 200)
                  controlPoint1:CGPointMake(kScreenWidth/2+50, 130)
                  controlPoint2:CGPointMake(kScreenWidth/2-50, 130)];
    
    // 绘制运动轨迹
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.path = bezierPath.CGPath;
    pathLayer.fillColor = [UIColor clearColor].CGColor;
    pathLayer.strokeColor = [UIColor clearColor].CGColor;
    pathLayer.lineWidth = 2.0f;

    [self.layer addSublayer:pathLayer];
    //  添加轨迹上的运动对象,以imageView为例
    CALayer *shipLayer = [CALayer layer];
    shipLayer.frame = CGRectMake(0, 0, 30, 30);
    shipLayer.position = CGPointMake(-50, 200);
    shipLayer.contents = (__bridge id)[UIImage imageNamed:@"obj"].CGImage;
    // 设置圆角
    shipLayer.cornerRadius = 15;
    shipLayer.masksToBounds = YES;
    // 设置运动物体的阴影效果
    shipLayer.shadowColor = [UIColor redColor].CGColor;
    shipLayer.shadowOffset = CGSizeMake(0, 5);
    shipLayer.shadowOpacity = 0.3f;
    [self.layer addSublayer:shipLayer];


    // 设置移动的动画1
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    // 运动周期4秒
    animation.duration = 4.0f;
    // 重复次数
    animation.repeatCount = 1;
    // 如果设置path 则values将被忽略
    animation.path = bezierPath.CGPath;
    
    // 0~2秒半周期内 使运动物体放大
    CABasicAnimation *animationZoomIn=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animationZoomIn.duration=2.0f;
    // 平滑过渡
    animationZoomIn.autoreverses=YES;
    animationZoomIn.repeatCount=1;
   // 缩放比例 
    animationZoomIn.toValue=[NSNumber numberWithFloat:1.56];
    animationZoomIn.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    // 2~4秒半周期内 使运动物体缩小
    CABasicAnimation *animationZoomOut=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    // 缩小动画的开启时间
    animationZoomOut.beginTime=2.0f;
    animationZoomOut.duration=2.0f;
    // 平滑过渡
    animationZoomOut.autoreverses=YES;
    animationZoomOut.repeatCount=1;
    // 缩放比例
    animationZoomOut.toValue=[NSNumber numberWithFloat:1];
    animationZoomOut.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
     
    // 使用CAAnimationGroup添加以上3个动画
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[animation, animationZoomIn,animationZoomOut];
    group.duration = 4.0f;
    
    [shipLayer addAnimation:group forKey:nil];
效果图.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,595评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,152评论 5 13
  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,181评论 1 23
  • 本文转载自:http://www.cocoachina.com/ios/20150105/10812.html 为...
    idiot_lin阅读 1,308评论 0 1
  • 显式动画 显式动画,它能够对一些属性做指定的自定义动画,或者创建非线性动画,比如沿着任意一条曲线移动。 属性动画 ...
    清风沐沐阅读 1,991评论 1 5