https://blog.csdn.net/qiwenmingshiwo/article/details/75806637
Path of the moving particle effects
a description
1 Effect
2 analysis step
two Code
1 VCViewh
2 VCViewm
. 3 ViewControllerm
particle effects - path of movement
a described
1.1 effect
results in FIG.
1.2 Analysis step
we need the above effect may be operated according to the following steps:
The first step: we need to create a View to support our this effect (VCView)
Step Two: We need to add a gesture to create a path, to record the movement of the gesture, and to achieve our drawing functions
The third step: to add particles used in the replication layer
Need to support replication layer, then we in this View (VCView) layer of layer should be copied
+ (Class) the layerClass {
// replication layer
return [CAReplicatorLayer class];
}
. 1
2
. 3
. 4
to create a particle, and the particles were added to the replication layer
//添加粒子
CALayer *dotL = [CALayer layer];
dotL.frame = CGRectMake(-20, 0, 20, 20);
dotL.backgroundColor = [UIColor redColor].CGColor;
self.dotLayer = dotL;
[self.layer addSublayer:dotL];
1
2
3
4
5
6
复制粒子
// Copy particles
CAReplicatorLayer REPL = * (* CAReplicatorLayer) self.layer;
repL.instanceCount = 30;
repL.instanceDelay = 0.2;
. 1
2
. 3
. 4
Step 4: Add the animation
Step Five: Implement re-drawing function
Note: We are using a custom VCView
Second, the code
2.1 VCView.h
//
// VCView.h
// 03_UIView77_ particle effects 1
//
// the Created by Qi civilization 17/7/22 ON.
// Copyright © 2017 Nian Qi civilization. All Rights Reserved.
/ /
#import <UIKit/UIKit.h>
VCView @interface: the UIView
// Start the animation
- (void) Start;
// redraw
- (void) REDRAW;
@end
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
2.2 VCView.m
//
VCView.m //
// 03_UIView77_ particle effects 1
//
// the Created by Qi civilization 17/7/22 ON.
// Copyright © 2017 Nian Qi civilization. All Rights Reserved.
//
#import "VCView.h"
@interface VCView()
@property(nonatomic,strong)UIBezierPath *path;
@property(nonatomic,strong)CALayer *dotLayer;
@end
@implementation VCView
+(Class)layerClass{
//复制层
return [CAReplicatorLayer class];
}
// start animation
- (void) Start {
// create a frame animation
CAKeyframeAnimation * Anim = [CAKeyframeAnimation Animation];
anim.keyPath = @ "position";
anim.path = self.path.CGPath;
anim.repeatCount = MAXFLOAT;
Anim =. 6 .duration;
[self.dotLayer the addAnimation: Anim forKey: nil];
}
// redraw
- (void) {REDRAW
// delete all movies
[self.dotLayer removeAllAnimations];
// clear path
[self.path removeAllPoints];
// Redraw
[Self the setNeedsDisplay];
}
-(void)awakeFromNib{
//添加手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
//添加粒子
CALayer *dotL = [CALayer layer];
dotL.frame = CGRectMake(-20, 0, 20, 20);
dotL.backgroundColor = [UIColor redColor].CGColor;
self.dotLayer = dotL;
[self.layer addSublayer:dotL];
//复制粒子
CAReplicatorLayer *repL = (CAReplicatorLayer*)self.layer;
repL.instanceCount = 30;
repL.instanceDelay = 0.2;
// create a path
self.path = [UIBezierPath bezierPath];
}
- (void) PAN: (UIPanGestureRecognizer *) PAN {
// current point or finger
CGPoint curentP = [pan locationInView: self ];
// gesture, moving to the starting point
IF (pan.state == UIGestureRecognizerStateBegan) {
[self.path moveToPoint: curentP];
} the else IF (pan.state == UIGestureRecognizerStateChanged) {
// add the points
[self.path addLineToPoint : curentP];
// redraw
[Self the setNeedsDisplay];
}
}
-(void)drawRect:(CGRect)rect{
[self.path stroke];
}
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 is
58
59
60
61 is
62 is
63 is
64
65
66
67
68
69
70
71 is
72
73 is
74
75
76
77
78
79
80
81
82
83
84
85
86
87
2.3 ViewController.m
//
// ViewController.m
// 03_UIView77_ particle effects 1
/ /
// the Created by Qi civilization 17/7/22 ON.
// Copyright © 2017 Nian Qi civilization. All Rights Reserved.
//
#import "ViewController.h"
#import "VCView.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet VCView *vcView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)start:(id)sender {
[self.vcView start];
}
- (IBAction)reDraw:(id)sender {
[self.vcView reDraw];
}
@end
----------------
Disclaimer: This article is CSDN blogger "Angry Bob" original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qiwenmingshiwo/article/details/75806637