CABasicAnimation - 核心动画里与CAShapeLayer相关的几个动画

 

CAShapeLayer有strokeEnd strokeStart lineWidth 三个属性。

 

先添加shapeLayer

 

 

- (CAShapeLayer *)shapeLayer {

    

    if (_shapeLayer == nil) {

        

        CGPoint startPoint = CGPointMake(50, 300);

        CGPoint endPoint = CGPointMake(300, 300);

        CGPoint controlPoint1 = CGPointMake(125, 200);

        CGPoint controlPoint2 = CGPointMake(225, 400);

        

        UIBezierPath *path = [[UIBezierPath alloc] init];

        _shapeLayer = [[CAShapeLayer alloc] init];

        

        [path moveToPoint:startPoint];

        [path addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];

        

        _shapeLayer.path = path.CGPath;

        _shapeLayer.fillColor = [UIColor clearColor].CGColor;

        _shapeLayer.strokeColor = [UIColor blackColor].CGColor;

        [self.view.layer addSublayer:_shapeLayer];

    }

    

    return _shapeLayer;

}

strokeEnd 这个属性的值范围是0-1,动画显示了从0到1之间每一个值对这条曲线的影响。

 

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

    animation.fromValue = @0;

    animation.toValue = @1;

    animation.duration = 2;

 

    [self.shapeLayer addAnimation:animation forKey:@""];

 

 

strokeStart 如果把这两个值首先都设置成0.5然后慢慢改变成0和1的时候就会变成第二个动画。

 

 

    self.shapeLayer.strokeStart = 0.5;

    self.shapeLayer.strokeEnd = 0.5;

 

    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];

    animation1.fromValue = @0.5;

    animation1.toValue = @0;

    animation1.duration = 2;

 

    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

    animation2.fromValue = @0.5;

    animation2.toValue = @1;

    animation2.duration = 2;

 

    [self.shapeLayer addAnimation:animation1 forKey:@""];

    [self.shapeLayer addAnimation:animation2 forKey:@""];

 

lineWidth 曲线会慢慢变粗

 

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"lineWidth"];

    animation.fromValue = @1;

    animation.toValue = @10;

    animation.duration = 2;

    [self.shapeLayer addAnimation:animation forKey:@""];

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值