forked from svga/SVGAPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGAPlayer.m
More file actions
233 lines (206 loc) · 7.67 KB
/
SVGAPlayer.m
File metadata and controls
233 lines (206 loc) · 7.67 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// SVGAPlayer.m
// SVGAPlayer
//
// Created by 崔明辉 on 16/6/17.
// Copyright © 2016年 UED Center. All rights reserved.
//
#import "SVGAPlayer.h"
#import "SVGAVideoEntity.h"
#import "SVGAVideoSpriteEntity.h"
#import "SVGAVideoSpriteFrameEntity.h"
#import "SVGAContentLayer.h"
#import "SVGAVectorLayer.h"
@interface SVGAPlayer () {
int _loopCount;
}
@property (nonatomic, strong) CALayer *drawLayer;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) NSInteger currentFrame;
@property (nonatomic, copy) NSDictionary *dynamicObjects;
@property (nonatomic, copy) NSDictionary *dynamicLayers;
@property (nonatomic, copy) NSDictionary *dynamicTexts;
@end
@implementation SVGAPlayer
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if (newSuperview == nil) {
[self stopAnimation:YES];
}
}
- (void)startAnimation {
[self stopAnimation:NO];
_loopCount = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
self.displayLink.frameInterval = 60 / self.videoItem.FPS;
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)pauseAnimation {
[self stopAnimation:NO];
}
- (void)stopAnimation {
[self stopAnimation:self.clearsAfterStop];
}
- (void)stopAnimation:(BOOL)clear {
if (![self.displayLink isPaused]) {
[self.displayLink setPaused:YES];
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
if (clear) {
[self clear];
}
self.displayLink = nil;
}
- (void)clear {
[self.drawLayer removeFromSuperlayer];
}
- (void)stepToFrame:(NSInteger)frame andPlay:(BOOL)andPlay {
if (frame >= self.videoItem.frames || frame < 0) {
return;
}
[self pauseAnimation];
self.currentFrame = frame;
[self update];
if (andPlay) {
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
self.displayLink.frameInterval = 60 / self.videoItem.FPS;
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
}
- (void)stepToPercentage:(CGFloat)percentage andPlay:(BOOL)andPlay {
NSInteger frame = (NSInteger)(self.videoItem.frames * percentage);
if (frame >= self.videoItem.frames && frame > 0) {
frame = self.videoItem.frames - 1;
}
[self stepToFrame:frame andPlay:andPlay];
}
- (void)draw {
self.drawLayer = [[CALayer alloc] init];
self.drawLayer.frame = CGRectMake(0, 0, self.videoItem.videoSize.width, self.videoItem.videoSize.height);
self.drawLayer.masksToBounds = true;
[self.videoItem.sprites enumerateObjectsUsingBlock:^(SVGAVideoSpriteEntity * _Nonnull sprite, NSUInteger idx, BOOL * _Nonnull stop) {
UIImage *bitmap;
if (sprite.imageKey != nil) {
if (self.dynamicObjects[sprite.imageKey] != nil) {
bitmap = self.dynamicObjects[sprite.imageKey];
}
else {
bitmap = self.videoItem.images[sprite.imageKey];
}
}
SVGAContentLayer *contentLayer = [sprite requestLayerWithBitmap:bitmap];
[self.drawLayer addSublayer:contentLayer];
if (sprite.imageKey != nil) {
if (self.dynamicLayers[sprite.imageKey] != nil) {
CALayer *dynamicLayer = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.dynamicLayers[sprite.imageKey]]];
dynamicLayer.contentsGravity = kCAGravityResizeAspect;
[contentLayer addSublayer:dynamicLayer];
}
if (self.dynamicTexts[sprite.imageKey] != nil) {
NSAttributedString *text = self.dynamicTexts[sprite.imageKey];
CGSize size = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
CATextLayer *textLayer = [CATextLayer layer];
textLayer.contentsScale = [[UIScreen mainScreen] scale];
[textLayer setString:self.dynamicTexts[sprite.imageKey]];
textLayer.frame = CGRectMake(0, 0, size.width, size.height);
[contentLayer addSublayer:textLayer];
}
}
}];
[self.layer addSublayer:self.drawLayer];
self.currentFrame = 0;
[self update];
[self resize];
}
- (void)resize {
CGFloat ratio = self.frame.size.width / self.videoItem.videoSize.width;
CGPoint offset = CGPointMake((1.0 - ratio) / 2.0 * self.videoItem.videoSize.width, (1 - ratio) / 2.0 * self.videoItem.videoSize.height);
self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(ratio, 0, 0, ratio, -offset.x, -offset.y));
}
- (void)layoutSubviews {
[super layoutSubviews];
[self resize];
}
- (void)update {
[CATransaction setDisableActions:YES];
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]]) {
[layer stepToFrame:self.currentFrame];
}
}
[CATransaction setDisableActions:NO];
}
- (void)next {
self.currentFrame++;
if (self.currentFrame >= self.videoItem.frames) {
self.currentFrame = 0;
_loopCount++;
if (self.loops > 0 && _loopCount >= self.loops) {
[self stopAnimation];
id delegate = self.delegate;
if (delegate != nil && [delegate respondsToSelector:@selector(svgaPlayerDidFinishedAnimation:)]) {
[delegate svgaPlayerDidFinishedAnimation:self];
}
}
}
[self update];
id delegate = self.delegate;
if (delegate != nil && [delegate respondsToSelector:@selector(svgaPlayerDidAnimatedToFrame:)]) {
[delegate svgaPlayerDidAnimatedToFrame:self.currentFrame];
}
if (delegate != nil && [delegate respondsToSelector:@selector(svgaPlayerDidAnimatedToPercentage:)] && self.videoItem.frames > 0) {
[delegate svgaPlayerDidAnimatedToPercentage:(CGFloat)(self.currentFrame + 1) / (CGFloat)self.videoItem.frames];
}
}
- (void)setVideoItem:(SVGAVideoEntity *)videoItem {
_videoItem = videoItem;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self clear];
[self draw];
}];
}
#pragma mark - Dynamic Object
- (void)setImage:(UIImage *)image forKey:(NSString *)aKey referenceLayer:(CALayer *)referenceLayer {
if (image == nil) {
return;
}
NSMutableDictionary *mutableDynamicObjects = [self.dynamicObjects mutableCopy];
[mutableDynamicObjects setObject:image forKey:aKey];
self.dynamicObjects = mutableDynamicObjects;
if (referenceLayer != nil) {
NSMutableDictionary *mutableDynamicLayers = [self.dynamicLayers mutableCopy];
[mutableDynamicLayers setObject:referenceLayer forKey:aKey];
self.dynamicLayers = mutableDynamicLayers;
}
}
- (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString *)aKey {
if (attributedText == nil) {
return;
}
NSMutableDictionary *mutableDynamicTexts = [self.dynamicTexts mutableCopy];
[mutableDynamicTexts setObject:attributedText forKey:aKey];
self.dynamicTexts = mutableDynamicTexts;
}
- (void)clearDynamicObjects {
self.dynamicObjects = nil;
self.dynamicLayers = nil;
}
- (NSDictionary *)dynamicObjects {
if (_dynamicObjects == nil) {
_dynamicObjects = @{};
}
return _dynamicObjects;
}
- (NSDictionary *)dynamicLayers {
if (_dynamicLayers == nil) {
_dynamicLayers = @{};
}
return _dynamicLayers;
}
- (NSDictionary *)dynamicTexts {
if (_dynamicTexts == nil) {
_dynamicTexts = @{};
}
return _dynamicTexts;
}
@end