@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<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 清空畫(huà)線
[_shapeLayer removeFromSuperlayer];
UITouch *touch = [touches anyObject];
// 獲取手指的觸摸點(diǎn)
CGPoint curP = [touch locationInView:self];
[self.path moveToPoint:curP];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// 獲取手指的觸摸點(diǎn)
CGPoint curP = [touch locationInView:self];
[self.path addLineToPoint:curP];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[self.path stroke];
}
// 跟隨路徑而動(dòng)畫(huà)
- (void)startAnim
{
self.shapeLayer.path = self.path.CGPath;
_shapeLayer.fillColor = [UIColor whiteColor].CGColor;
_shapeLayer.strokeColor = [UIColor redColor].CGColor;
_shapeLayer.strokeEnd = 1; //圖層畫(huà)百分之多少
[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];
// 清空畫(huà)線
[self.path removeAllPoints];
[self setNeedsDisplay];
}
@end
點(diǎn)擊按鈕開(kāi)始 描繪上面的路徑動(dòng)畫(huà)