-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPScaleLoadingView.m
More file actions
executable file
·208 lines (174 loc) · 9.37 KB
/
Copy pathPScaleLoadingView.m
File metadata and controls
executable file
·208 lines (174 loc) · 9.37 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// PScaleLoadingView.h
// testAnimation
//
// Created by xp_mac on 16/2/26.
// Copyright © 2016年 xp_mac. All rights reserved.
//
#import "PScaleLoadingView.h"
#import "CustomShapeLayer.h"
#import "DurModel.h"
#define DURATION_TIME 1
@interface PScaleLoadingView ()
@property (nonatomic, strong) NSMutableDictionary * layers;
@property (nonatomic, strong) NSMapTable * completionBlocks;
@end
@implementation PScaleLoadingView
#pragma mark - Animation init
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupProperties];
[self setupLayers];
}
return self;
}
- (void)setupProperties{
//初始化数据
self.completionBlocks = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsOpaqueMemory valueOptions:NSPointerFunctionsStrongMemory];;
self.layers = [NSMutableDictionary dictionary];
}
- (void)setupLayers{
//创建8个自定义的矩形框
for (int i = 0; i < 8; i++) {
NSString *shapeLayerName = [NSString stringWithFormat:@"rectangle%d",i+1];
CustomShapeLayer *rectangle = [[CustomShapeLayer alloc] initTheFrame:CGRectZero withIndex:i+1];
[self.layer addSublayer:rectangle];
self.layers[shapeLayerName] = rectangle;
}
CAShapeLayer * oval = [CAShapeLayer layer];
oval.frame = CGRectMake(0, 0, 2, 2);
oval.position = CGPointMake(self.center.x, self.center.y);
oval.backgroundColor = [UIColor colorWithRed:168/255.f green:169/255.f blue:38/255.f alpha:1.0].CGColor;
[self.layer addSublayer:oval];
}
#pragma mark - Animation Setup
- (void)customAnimations{
self.layer.speed = 1;
//创建8个动画模型
for (int i = 0; i < 8; i++) {
CABasicAnimation *transformAnimation = [self eachPartAnimation:i];
NSString *layerNameStr = [NSString stringWithFormat:@"rectangle%d",i+1];
if (i == 0) {
[self.layers[layerNameStr] addAnimation:transformAnimation forKey:nil];
[self performSelector:@selector(pauseLayer:) withObject:self.layers[layerNameStr] afterDelay:1];
[self performSelector:@selector(resumeLayer:) withObject:self.layers[layerNameStr] afterDelay:4.5];
}
else
{
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = @[transformAnimation];
animationGroup.duration = [DurModel initWithTheDuration:transformAnimation];
[self.layers[layerNameStr] addAnimation:animationGroup forKey:nil];
[self performSelector:@selector(pauseLayer:) withObject:self.layers[layerNameStr] afterDelay:1+(i)*0.5];
[self performSelector:@selector(resumeLayer:) withObject:self.layers[layerNameStr] afterDelay:4.5+(i)*0.5];
}
}
}
//暂停动画
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
//重新启动动画
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
//依次索引不同的动画效果
- (CABasicAnimation *) eachPartAnimation:(NSInteger)index
{
CABasicAnimation *animation = [CABasicAnimation animation];
switch (index) {
case 0:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0))];;
animation.duration = DURATION_TIME;
animation.beginTime = CACurrentMediaTime();
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
break;
case 1:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-45 * M_PI/180, 0, 0, -1)];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0)), CATransform3DMakeRotation(45 * M_PI/180, 0, -0, 1))];;
animation.duration = DURATION_TIME;
animation.beginTime = 0.5;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
break;
case 2:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-90 * M_PI/180, 0, 0, -1)];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0)), CATransform3DMakeRotation(90 * M_PI/180, 0, -0, 1))];;
animation.duration = DURATION_TIME;
animation.beginTime = 1;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
break;
case 3:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(225 * M_PI/180, 0, 0, -1)];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0)), CATransform3DMakeRotation(-225 * M_PI/180, -0, 0, 1))];;
animation.duration = DURATION_TIME;
animation.beginTime = 1.5;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
break;
case 4:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, -1)];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0)), CATransform3DMakeRotation(-M_PI, 0, 0, 1))];;
animation.duration = DURATION_TIME;
animation.beginTime = 2;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
break;
case 5:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(135 * M_PI/180, 0, 0, -1)];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0)), CATransform3DMakeRotation(-135 * M_PI/180, -0, 0, 1))];;
animation.duration = DURATION_TIME;
animation.beginTime = 2.5;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation.autoreverses = YES;
break;
case 6:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0, 0, -1)];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0)), CATransform3DMakeRotation(-M_PI_2, 0, 0, 1))];;
animation.duration = DURATION_TIME;
animation.beginTime = 3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation.autoreverses = YES;
break;
case 7:
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, -1)];;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DConcat(CATransform3DMakeScale(30, 30, 30), CATransform3DMakeTranslation(0, -60, 0)), CATransform3DMakeRotation(-M_PI_4, 0, 0, 1))];;
animation.duration = DURATION_TIME;
animation.beginTime = 3.5;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
animation.autoreverses = YES;
break;
default:
break;
}
return animation;
}
#pragma mark - Animation Cleanup
- (void)removeAllAnimations{
[self.layers enumerateKeysAndObjectsUsingBlock:^(id key, CALayer *layer, BOOL *stop) {
[layer removeAllAnimations];
}];
}
@end