美文网首页iOS技术专题
iOS CAKeyframeAnimation动画

iOS CAKeyframeAnimation动画

作者: 倪大头 | 来源:发表于2018-05-10 15:11 被阅读27次

    CAKeyframeAnimation有两种添加动画的方式
    1.value方式
    通过添加关键点描述动画

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //设置value
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(self.roadView.frame.size.width/4*3, self.view.frame.size.height - kScaleY*30 - self.carImg.frame.size.height/2)];
    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(self.roadView.frame.size.width/4*3, kScaleY*200)];
    animation.values = @[value1,value2];
    //重复次数 默认1
    animation.repeatCount = 1;
    //设置动画时间,时间内动画一直重复
    //animation.repeatDuration = 60;
    //是否原路返回 默认NO
    animation.autoreverses = NO;
    //移动速度,越小越快
    animation.duration = 4.0f;
    //动画速度变化
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //动画结束时动作
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
        
    [self.carImg.layer addAnimation:animation forKey:nil];
    

    2.path方式
    通过添加路径描述动画

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //CGPathRef对象,动画的路线
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, NULL, CGRectMake(kScaleX*50, kScaleY*150, kScaleX*280, kScaleY*400));
    animation.path = path;
    CGPathRelease(path);
    //重复次数 默认1
    animation.repeatCount = 1;
    //设置动画时间,时间内动画一直重复
    //animation.repeatDuration = 60;
    //是否原路返回 默认NO
    animation.autoreverses = NO;
    //移动速度,越小越快
    animation.duration = 4.0f;
    //动画速度变化
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //动画结束时动作
    animation.fillMode = kCAFillModeForwards;
    //根据路径自动旋转
    animation.rotationMode = kCAAnimationRotateAutoReverse;
    animation.removedOnCompletion = NO;
    [self.carImg.layer addAnimation:animation forKey:nil];
    

    相关文章

      网友评论

        本文标题:iOS CAKeyframeAnimation动画

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