UI进阶路径动画

UIBezierPath绘制路径
- (UIBezierPath *)path
{
    if (!_path) {
        _path = [UIBezierPath bezierPath];
    }
    return _path;
}

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

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 获取手触摸点
    UITouch *touch = [touches anyObject];
    CGPoint currentP = [touch locationInView:self];
    
    // 不断添加点到某根线
    [self.path addLineToPoint:currentP];
    // 绘制出来
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    // 把路径画出来
    [self.path stroke];
}




搞一个和路径一样大的图层,绘制到屏幕上
// 跟着路径动画
- (void)startAnim
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    
    layer.path = self.path.CGPath;
    // 填充颜色,最后一个封闭的形状
    layer.fillColor = [UIColor whiteColor].CGColor;
    // 描边颜色
    layer.strokeColor = [UIColor redColor].CGColor;
    
    // 只画一部分
//    layer.strokeEnd = 0.5;
    
    // 改变图层的某个熟悉
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"strokeEnd";
    anim.fromValue = @0;
    anim.toValue = @1;
    anim.duration = 5;
    [layer addAnimation:anim forKey:nil];
    [self.layer addSublayer:layer];
    
    // 清空画线,图形还在,和路径一样图层
    [self clearAll];
}

- (void)clearAll
{
    [self.path removeAllPoints];
    [self setNeedsDisplay];
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容