forked from svga/SVGAPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGAPlayerManager.m
More file actions
182 lines (151 loc) · 5.58 KB
/
SVGAPlayerManager.m
File metadata and controls
182 lines (151 loc) · 5.58 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
//
// SVGAPlayerManager.m
// SVGAPlayer
//
// Created by 崔明辉 on 2017/6/15.
// Copyright © 2017年 UED Center. All rights reserved.
//
#import "SVGAPlayerManager.h"
#import "SVGAPlayer.h"
#import "SVGAParser.h"
#import <objc/runtime.h>
@interface SVGAPlayer (React)<SVGAPlayerDelegate>
@property(nonatomic, copy) NSString *source;
@property(nonatomic, copy) NSString *currentState;
@property(nonatomic, assign) NSInteger toFrame;
@property(nonatomic, assign) NSInteger toPercentage;
@property(nonatomic, copy) RCTBubblingEventBlock onFinished;
@property(nonatomic, copy) RCTBubblingEventBlock onFrame;
@property(nonatomic, copy) RCTBubblingEventBlock onPercentage;
@end
@implementation SVGAPlayer (React)
static int kReactSourceIdentifier;
static int kReactCurrentStateIdentifier;
static int kReactOnFinishedIdentifier;
static int kReactOnFrameIdentifier;
static int kReactOnPercentageIdentifier;
- (void)loadWithSource:(NSString *)source {
SVGAParser *parser = [[SVGAParser alloc] init];
if ([source hasPrefix:@"http"] || [source hasPrefix:@"https"]) {
[parser parseWithURL:[NSURL URLWithString:source]
completionBlock:^(SVGAVideoEntity *_Nullable videoItem) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self setVideoItem:videoItem];
[self startAnimation];
}];
}
failureBlock:nil];
} else {
NSString *localPath = [[NSBundle mainBundle] pathForResource:source ofType:@"svga"];
if (localPath != nil) {
[parser parseWithData:[NSData dataWithContentsOfFile:localPath]
cacheKey:source
completionBlock:^(SVGAVideoEntity *_Nonnull videoItem) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self setVideoItem:videoItem];
[self startAnimation];
}];
}
failureBlock:nil];
}
}
}
- (void)setSource:(NSString *)source {
if ([source isKindOfClass:[NSString class]] && ([self source] == nil || ![source isEqualToString:[self source]])) {
objc_setAssociatedObject(self, &kReactSourceIdentifier, source, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self loadWithSource:source];
}
}
- (NSString *)source {
return objc_getAssociatedObject(self, &kReactSourceIdentifier);
}
- (void)setCurrentState:(NSString *)currentState {
if ([currentState isKindOfClass:[NSString class]] &&
([self currentState] == nil || ![currentState isEqualToString:[self currentState]])) {
objc_setAssociatedObject(self, &kReactCurrentStateIdentifier, currentState, OBJC_ASSOCIATION_COPY_NONATOMIC);
if ([currentState isEqualToString:@"start"]) {
[self startAnimation];
} else if ([currentState isEqualToString:@"pause"]) {
[self pauseAnimation];
} else if ([currentState isEqualToString:@"stop"]) {
[self stopAnimation];
} else if ([currentState isEqualToString:@"clear"]) {
[self stopAnimation];
[self clear];
}
}
}
- (NSString *)currentState {
return objc_getAssociatedObject(self, &kReactCurrentStateIdentifier);
}
- (void)setOnFinished:(RCTBubblingEventBlock)onFinished {
objc_setAssociatedObject(self, &kReactOnFinishedIdentifier, onFinished, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (RCTBubblingEventBlock)onFinished {
return objc_getAssociatedObject(self, &kReactOnFinishedIdentifier);
}
- (void)setOnFrame:(RCTBubblingEventBlock)onFrame {
objc_setAssociatedObject(self, &kReactOnFrameIdentifier, onFrame, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (RCTBubblingEventBlock)onFrame {
return objc_getAssociatedObject(self, &kReactOnFrameIdentifier);
}
- (void)setOnPercentage:(RCTBubblingEventBlock)onPercentage {
objc_setAssociatedObject(self, &kReactOnPercentageIdentifier, onPercentage, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (RCTBubblingEventBlock)onPercentage {
return objc_getAssociatedObject(self, &kReactOnPercentageIdentifier);
}
- (void)setToFrame:(NSInteger)toFrame {
if (toFrame < 0) {
return;
}
[self stepToFrame:toFrame andPlay:[self.currentState isEqualToString:@"play"]];
}
- (NSInteger)toFrame {
return 0;
}
- (void)setToPercentage:(NSInteger)toPercentage {
if (toPercentage < 0) {
return;
}
[self stepToPercentage:toPercentage andPlay:[self.currentState isEqualToString:@"play"]];
}
- (NSInteger)toPercentage {
return 0.0;
}
- (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player {
if (self.onFinished) {
self.onFinished(@{});
}
}
- (void)svgaPlayerDidAnimatedToFrame:(NSInteger)frame {
if (self.onFrame) {
self.onFrame(@{ @"value" : @(frame) });
}
}
- (void)svgaPlayerDidAnimatedToPercentage:(CGFloat)percentage {
if (self.onPercentage) {
self.onPercentage(@{ @"value" : @(percentage) });
}
}
@end
@interface SVGAPlayerManager ()
@end
@implementation SVGAPlayerManager
RCT_EXPORT_MODULE()
RCT_EXPORT_VIEW_PROPERTY(loops, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(clearsAfterStop, BOOL)
RCT_EXPORT_VIEW_PROPERTY(source, NSString)
RCT_EXPORT_VIEW_PROPERTY(currentState, NSString)
RCT_EXPORT_VIEW_PROPERTY(toFrame, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(toPercentage, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(onFinished, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onFrame, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPercentage, RCTBubblingEventBlock)
- (UIView *)view {
SVGAPlayer *aPlayer = [[SVGAPlayer alloc] init];
aPlayer.delegate = aPlayer;
return aPlayer;
}
@end