美文网首页
路径动画

路径动画

作者: 七里田间的守望者 | 来源:发表于2016-11-17 17:08 被阅读17次

    自定义view

    -懒加载路径

    @property (nonatomic, strong) UIBezierPath * path;
    - (UIBezierPath *)path
    {
        if (_path == nil) {
            _path = [UIBezierPath bezierPath];
            _path.lineWidth = 10;
        }
        return _path;
    }
    

    -触摸方法

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        UITouch * touch = [touches anyObject];
        
        //获取手指的触摸点
        CGPoint curP = [touch locationInView:self];
        
        [self.path moveToPoint:curP];
        
        [self setNeedsDisplay];
        
    }
    

    -移动手指把路径添加到path中

    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        UITouch * touch = [touches anyObject];
        
        //获取手指的触摸点
        CGPoint curP = [touch locationInView:self];
        
        [self.path addLineToPoint:curP];
        
        [self setNeedsDisplay];
    }
    

    -开始绘制

    - (void)drawRect:(CGRect)rect
    {
        [self.path stroke];
    }
    

    -开始动画点击

    - (void)startAnima
    {
        //跟随路径动画
        //图层
        CAShapeLayer * layer = [CAShapeLayer layer];
        
        layer.path = self.path.CGPath;
        
        layer.lineWidth = 10;
        
        layer.fillColor = [UIColor whiteColor].CGColor;
        
        layer.strokeColor = [UIColor redColor].CGColor;
        
        
        [self.layer addSublayer:layer];
        
        CABasicAnimation * anima = [CABasicAnimation animation];
        
        anima.keyPath = @"strokeEnd";
        anima.fromValue = @0;
        anima.toValue = @1;
        anima.duration = 5;
        
        [layer addAnimation:anima forKey:nil];
        
        //清空划线
        [self.path removeAllPoints];
        
    }
    

    相关文章

      网友评论

          本文标题:路径动画

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