路径动画

@interface HJHPullDownView : UIView
- (void)startAnim;

@end
@interface HJHPullDownView ()

@property (nonatomic ,retain) UIBezierPath *path;
@property (nonatomic ,retain) CAShapeLayer * shapeLayer ;
@end

@implementation HJHPullDownView

- (UIBezierPath *)path
{
    if (!_path)
    {
        _path = [UIBezierPath bezierPath];
    }
    return _path;
}
- (CAShapeLayer *)shapeLayer
{
    if (!_shapeLayer)
    {
        _shapeLayer = [CAShapeLayer layer];
    }
    return _shapeLayer;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 清空画线
    [_shapeLayer removeFromSuperlayer];
    
    UITouch *touch = [touches anyObject];
    // 获取手指的触摸点
    CGPoint curP = [touch locationInView:self];
    
    [self.path moveToPoint:curP];
}
- (void)touchesMoved:(NSSet *)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)startAnim
{    
    self.shapeLayer.path = self.path.CGPath;
    
    _shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    
    _shapeLayer.strokeColor = [UIColor redColor].CGColor;
    
    _shapeLayer.strokeEnd = 1; //图层画百分之多少
    
    [self.layer addSublayer:_shapeLayer];
    
    CABasicAnimation *anim = [CABasicAnimation animation];
    
    anim.keyPath = @"strokeEnd";
    
    anim.fromValue = @0;
    
    anim.toValue = @1;
    
    anim.duration = 2;
    
    [_shapeLayer addAnimation:anim forKey:nil];
    
    
    // 清空画线
    [self.path removeAllPoints];
    
    [self setNeedsDisplay];
}
@end

路径动画_第1张图片
621EF14B-F26B-4B5E-B746-8FBB0191861F.png

点击按钮开始 描绘上面的路径动画

路径动画_第2张图片
51AE4F8C-32D5-4CA3-82FB-7A603A8A412A.png

你可能感兴趣的:(路径动画)