1 #import "VCView.h" 2 3 @interface VCView() 4 5 6 @property(nonatomic,strong) UIBezierPath *path; 7 8 @property(nonatomic,weak) CALayer *dotLayer ; 9 10 @end 11 12 13 @implementation VCView 14 15 16 +(nonnull Class)layerClass{ 17 18 return [CAReplicatorLayer class]; 19 } 20 21 22 23 -(void)awakeFromNib{ 24 25 //添加手势 26 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 27 28 [self addGestureRecognizer:pan]; 29 30 31 //创建路径 32 UIBezierPath *path = [UIBezierPath bezierPath]; 33 34 self.path = path; 35 36 37 38 //创建粒子 39 CALayer *dotLayer = [CALayer layer]; 40 dotLayer.frame = CGRectMake(-10, 0, 10, 10); 41 dotLayer.backgroundColor = [UIColor redColor].CGColor; 42 self.dotLayer = dotLayer; 43 [self.layer addSublayer:dotLayer]; 44 45 46 47 CAReplicatorLayer *repL = (CAReplicatorLayer *)self.layer; 48 repL.instanceCount = 30; 49 repL.instanceDelay = 0.25; 50 51 52 53 54 } 55 56 //开始 57 - (void)start{ 58 59 //添加动画. 60 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; 61 anim.keyPath = @"position"; 62 anim.path = self.path.CGPath; 63 anim.repeatCount = MAXFLOAT; 64 anim.duration = 4; 65 [self.dotLayer addAnimation:anim forKey:nil]; 66 67 } 68 69 //重绘 70 - (void)reDraw{ 71 72 //把路径清空 73 [self.path removeAllPoints]; 74 [self setNeedsDisplay]; 75 76 //移除动画 77 [self.dotLayer removeAllAnimations]; 78 79 80 81 82 } 83 84 85 86 87 - (void)pan:(UIPanGestureRecognizer *)pan{ 88 89 //开始绘制 90 91 //获取手指当前的点 92 CGPoint curP = [pan locationInView:self]; 93 if(pan.state == UIGestureRecognizerStateBegan){ 94 95 [self.path moveToPoint:curP]; 96 97 }else if(pan.state == UIGestureRecognizerStateChanged){ 98 99 [self.path addLineToPoint:curP]; 100 101 //重绘 102 [self setNeedsDisplay]; 103 } 104 105 106 107 } 108 109 110 111 112 113 // Only override drawRect: if you perform custom drawing. 114 // An empty implementation adversely affects performance during animation. 115 - (void)drawRect:(CGRect)rect { 116 117 [self.path stroke]; 118 119 120 } 121 122 123 @end
//详见粒子效果代码
浙公网安备 33010602011771号