美文网首页
CABasicAnimation绘制缩放核心动画效果

CABasicAnimation绘制缩放核心动画效果

作者: 金丝楠 | 来源:发表于2017-04-20 16:22 被阅读0次

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

相关文章

网友评论

      本文标题:CABasicAnimation绘制缩放核心动画效果

      本文链接:https://www.haomeiwen.com/subject/zvelzttx.html