SVGAPlayer.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. //
  2. // SVGAPlayer.m
  3. // SVGAPlayer
  4. //
  5. // Created by 崔明辉 on 16/6/17.
  6. // Copyright © 2016年 UED Center. All rights reserved.
  7. //
  8. #import "SVGAPlayer.h"
  9. #import "SVGAVideoEntity.h"
  10. #import "SVGAVideoSpriteEntity.h"
  11. #import "SVGAVideoSpriteFrameEntity.h"
  12. #import "SVGAContentLayer.h"
  13. #import "SVGABitmapLayer.h"
  14. #import "SVGAVectorLayer.h"
  15. #import "SVGAAudioLayer.h"
  16. #import "SVGAAudioEntity.h"
  17. @interface SVGAPlayer ()
  18. @property (nonatomic, strong) CALayer *drawLayer;
  19. @property (nonatomic, strong) NSArray<SVGAAudioLayer *> *audioLayers;
  20. @property (nonatomic, strong) CADisplayLink *displayLink;
  21. @property (nonatomic, assign) NSInteger currentFrame;
  22. @property (nonatomic, copy) NSArray *contentLayers;
  23. @property (nonatomic, copy) NSDictionary<NSString *, UIImage *> *dynamicObjects;
  24. @property (nonatomic, copy) NSDictionary<NSString *, NSAttributedString *> *dynamicTexts;
  25. @property (nonatomic, copy) NSDictionary<NSString *, SVGAPlayerDynamicDrawingBlock> *dynamicDrawings;
  26. @property (nonatomic, copy) NSDictionary<NSString *, NSNumber *> *dynamicHiddens;
  27. @property (nonatomic, assign) int loopCount;
  28. @property (nonatomic, assign) NSRange currentRange;
  29. @property (nonatomic, assign) BOOL forwardAnimating;
  30. @property (nonatomic, assign) BOOL reversing;
  31. @end
  32. @implementation SVGAPlayer
  33. - (instancetype)init {
  34. if (self = [super init]) {
  35. [self initPlayer];
  36. }
  37. return self;
  38. }
  39. - (instancetype)initWithFrame:(CGRect)frame {
  40. if (self = [super initWithFrame:frame]) {
  41. [self initPlayer];
  42. }
  43. return self;
  44. }
  45. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  46. if (self = [super initWithCoder:aDecoder]) {
  47. [self initPlayer];
  48. }
  49. return self;
  50. }
  51. - (void)initPlayer {
  52. self.contentMode = UIViewContentModeTop;
  53. self.clearsAfterStop = YES;
  54. }
  55. - (void)willMoveToSuperview:(UIView *)newSuperview {
  56. [super willMoveToSuperview:newSuperview];
  57. if (newSuperview == nil) {
  58. [self stopAnimation:YES];
  59. }
  60. }
  61. - (void)startAnimation {
  62. if (self.videoItem == nil) {
  63. NSLog(@"videoItem could not be nil!");
  64. return;
  65. } else if (self.drawLayer == nil) {
  66. self.videoItem = _videoItem;
  67. }
  68. [self stopAnimation:NO];
  69. self.loopCount = 0;
  70. if (self.videoItem.FPS == 0) {
  71. NSLog(@"videoItem FPS could not be 0!");
  72. return;
  73. }
  74. self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
  75. self.displayLink.frameInterval = 60 / self.videoItem.FPS;
  76. [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mainRunLoopMode];
  77. self.forwardAnimating = !self.reversing;
  78. }
  79. - (void)startAnimationWithRange:(NSRange)range reverse:(BOOL)reverse {
  80. if (self.videoItem == nil) {
  81. NSLog(@"videoItem could not be nil!");
  82. return;
  83. } else if (self.drawLayer == nil) {
  84. self.videoItem = _videoItem;
  85. }
  86. [self stopAnimation:NO];
  87. self.loopCount = 0;
  88. if (self.videoItem.FPS == 0) {
  89. NSLog(@"videoItem FPS could not be 0!");
  90. return;
  91. }
  92. self.currentRange = range;
  93. self.reversing = reverse;
  94. if (reverse) {
  95. self.currentFrame = MIN(self.videoItem.frames - 1, range.location + range.length - 1);
  96. }
  97. else {
  98. self.currentFrame = MAX(0, range.location);
  99. }
  100. self.forwardAnimating = !self.reversing;
  101. self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
  102. self.displayLink.frameInterval = 60 / self.videoItem.FPS;
  103. [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mainRunLoopMode];
  104. }
  105. - (void)pauseAnimation {
  106. [self stopAnimation:NO];
  107. }
  108. - (void)stopAnimation {
  109. [self stopAnimation:self.clearsAfterStop];
  110. }
  111. - (void)stopAnimation:(BOOL)clear {
  112. self.forwardAnimating = NO;
  113. if (self.displayLink != nil) {
  114. [self.displayLink invalidate];
  115. }
  116. if (clear) {
  117. [self clear];
  118. }
  119. [self clearAudios];
  120. self.displayLink = nil;
  121. }
  122. - (void)clear {
  123. self.contentLayers = nil;
  124. [self.drawLayer removeFromSuperlayer];
  125. self.drawLayer = nil;
  126. }
  127. - (void)clearAudios {
  128. for (SVGAAudioLayer *layer in self.audioLayers) {
  129. if (layer.audioPlaying) {
  130. [layer.audioPlayer stop];
  131. layer.audioPlaying = NO;
  132. }
  133. }
  134. }
  135. - (void)stepToFrame:(NSInteger)frame andPlay:(BOOL)andPlay {
  136. if (self.videoItem == nil) {
  137. NSLog(@"videoItem could not be nil!");
  138. return;
  139. } else if (self.drawLayer == nil) {
  140. self.videoItem = _videoItem;
  141. }
  142. if (frame >= self.videoItem.frames || frame < 0) {
  143. return;
  144. }
  145. [self pauseAnimation];
  146. self.currentFrame = frame;
  147. [self update];
  148. if (andPlay) {
  149. self.forwardAnimating = YES;
  150. if (self.videoItem.FPS == 0) {
  151. NSLog(@"videoItem FPS could not be 0!");
  152. return;
  153. }
  154. self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];
  155. self.displayLink.frameInterval = 60 / self.videoItem.FPS;
  156. [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:self.mainRunLoopMode];
  157. }
  158. }
  159. - (void)stepToPercentage:(CGFloat)percentage andPlay:(BOOL)andPlay {
  160. NSInteger frame = (NSInteger)(self.videoItem.frames * percentage);
  161. if (frame >= self.videoItem.frames && frame > 0) {
  162. frame = self.videoItem.frames - 1;
  163. }
  164. [self stepToFrame:frame andPlay:andPlay];
  165. }
  166. - (void)draw {
  167. self.drawLayer = [[CALayer alloc] init];
  168. self.drawLayer.frame = CGRectMake(0, 0, self.videoItem.videoSize.width, self.videoItem.videoSize.height);
  169. self.drawLayer.masksToBounds = true;
  170. NSMutableDictionary *tempHostLayers = [NSMutableDictionary dictionary];
  171. NSMutableArray *tempContentLayers = [NSMutableArray array];
  172. [self.videoItem.sprites enumerateObjectsUsingBlock:^(SVGAVideoSpriteEntity * _Nonnull sprite, NSUInteger idx, BOOL * _Nonnull stop) {
  173. UIImage *bitmap;
  174. if (sprite.imageKey != nil) {
  175. NSString *bitmapKey = [sprite.imageKey stringByDeletingPathExtension];
  176. if (self.dynamicObjects[bitmapKey] != nil) {
  177. bitmap = self.dynamicObjects[bitmapKey];
  178. }
  179. else {
  180. bitmap = self.videoItem.images[bitmapKey];
  181. }
  182. }
  183. SVGAContentLayer *contentLayer = [sprite requestLayerWithBitmap:bitmap];
  184. contentLayer.imageKey = sprite.imageKey;
  185. [tempContentLayers addObject:contentLayer];
  186. if ([sprite.imageKey hasSuffix:@".matte"]) {
  187. CALayer *hostLayer = [[CALayer alloc] init];
  188. hostLayer.mask = contentLayer;
  189. tempHostLayers[sprite.imageKey] = hostLayer;
  190. } else {
  191. if (sprite.matteKey && sprite.matteKey.length > 0) {
  192. CALayer *hostLayer = tempHostLayers[sprite.matteKey];
  193. [hostLayer addSublayer:contentLayer];
  194. if (![sprite.matteKey isEqualToString:self.videoItem.sprites[idx - 1].matteKey]) {
  195. [self.drawLayer addSublayer:hostLayer];
  196. }
  197. } else {
  198. [self.drawLayer addSublayer:contentLayer];
  199. }
  200. }
  201. if (sprite.imageKey != nil) {
  202. if (self.dynamicTexts[sprite.imageKey] != nil) {
  203. NSAttributedString *text = self.dynamicTexts[sprite.imageKey];
  204. CGSize bitmapSize = CGSizeMake(self.videoItem.images[sprite.imageKey].size.width * self.videoItem.images[sprite.imageKey].scale, self.videoItem.images[sprite.imageKey].size.height * self.videoItem.images[sprite.imageKey].scale);
  205. CGSize size = [text boundingRectWithSize:bitmapSize
  206. options:NSStringDrawingUsesLineFragmentOrigin
  207. context:NULL].size;
  208. CATextLayer *textLayer = [CATextLayer layer];
  209. textLayer.contentsScale = [[UIScreen mainScreen] scale];
  210. [textLayer setString:self.dynamicTexts[sprite.imageKey]];
  211. textLayer.frame = CGRectMake(0, 0, size.width, size.height);
  212. [contentLayer addSublayer:textLayer];
  213. contentLayer.textLayer = textLayer;
  214. [contentLayer resetTextLayerProperties:text];
  215. }
  216. if (self.dynamicHiddens[sprite.imageKey] != nil &&
  217. [self.dynamicHiddens[sprite.imageKey] boolValue] == YES) {
  218. contentLayer.dynamicHidden = YES;
  219. }
  220. if (self.dynamicDrawings[sprite.imageKey] != nil) {
  221. contentLayer.dynamicDrawingBlock = self.dynamicDrawings[sprite.imageKey];
  222. }
  223. }
  224. }];
  225. self.contentLayers = tempContentLayers;
  226. [self.layer addSublayer:self.drawLayer];
  227. NSMutableArray *audioLayers = [NSMutableArray array];
  228. [self.videoItem.audios enumerateObjectsUsingBlock:^(SVGAAudioEntity * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  229. SVGAAudioLayer *audioLayer = [[SVGAAudioLayer alloc] initWithAudioItem:obj videoItem:self.videoItem];
  230. [audioLayers addObject:audioLayer];
  231. }];
  232. self.audioLayers = audioLayers;
  233. [self update];
  234. [self resize];
  235. }
  236. - (void)resize {
  237. if (self.contentMode == UIViewContentModeScaleAspectFit) {
  238. CGFloat videoRatio = self.videoItem.videoSize.width / self.videoItem.videoSize.height;
  239. CGFloat layerRatio = self.bounds.size.width / self.bounds.size.height;
  240. if (videoRatio > layerRatio) {
  241. CGFloat ratio = self.bounds.size.width / self.videoItem.videoSize.width;
  242. CGPoint offset = CGPointMake(
  243. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.width,
  244. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.height
  245. - (self.bounds.size.height - self.videoItem.videoSize.height * ratio) / 2.0
  246. );
  247. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(ratio, 0, 0, ratio, -offset.x, -offset.y));
  248. }
  249. else {
  250. CGFloat ratio = self.bounds.size.height / self.videoItem.videoSize.height;
  251. CGPoint offset = CGPointMake(
  252. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.width - (self.bounds.size.width - self.videoItem.videoSize.width * ratio) / 2.0,
  253. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.height);
  254. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(ratio, 0, 0, ratio, -offset.x, -offset.y));
  255. }
  256. }
  257. else if (self.contentMode == UIViewContentModeScaleAspectFill) {
  258. CGFloat videoRatio = self.videoItem.videoSize.width / self.videoItem.videoSize.height;
  259. CGFloat layerRatio = self.bounds.size.width / self.bounds.size.height;
  260. if (videoRatio < layerRatio) {
  261. CGFloat ratio = self.bounds.size.width / self.videoItem.videoSize.width;
  262. CGPoint offset = CGPointMake(
  263. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.width,
  264. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.height
  265. - (self.bounds.size.height - self.videoItem.videoSize.height * ratio) / 2.0
  266. );
  267. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(ratio, 0, 0, ratio, -offset.x, -offset.y));
  268. }
  269. else {
  270. CGFloat ratio = self.bounds.size.height / self.videoItem.videoSize.height;
  271. CGPoint offset = CGPointMake(
  272. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.width - (self.bounds.size.width - self.videoItem.videoSize.width * ratio) / 2.0,
  273. (1.0 - ratio) / 2.0 * self.videoItem.videoSize.height);
  274. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(ratio, 0, 0, ratio, -offset.x, -offset.y));
  275. }
  276. }
  277. else if (self.contentMode == UIViewContentModeTop) {
  278. CGFloat scaleX = self.frame.size.width / self.videoItem.videoSize.width;
  279. CGPoint offset = CGPointMake((1.0 - scaleX) / 2.0 * self.videoItem.videoSize.width, (1 - scaleX) / 2.0 * self.videoItem.videoSize.height);
  280. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(scaleX, 0, 0, scaleX, -offset.x, -offset.y));
  281. }
  282. else if (self.contentMode == UIViewContentModeBottom) {
  283. CGFloat scaleX = self.frame.size.width / self.videoItem.videoSize.width;
  284. CGPoint offset = CGPointMake(
  285. (1.0 - scaleX) / 2.0 * self.videoItem.videoSize.width,
  286. (1.0 - scaleX) / 2.0 * self.videoItem.videoSize.height);
  287. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(scaleX, 0, 0, scaleX, -offset.x, -offset.y + self.frame.size.height - self.videoItem.videoSize.height * scaleX));
  288. }
  289. else if (self.contentMode == UIViewContentModeLeft) {
  290. CGFloat scaleY = self.frame.size.height / self.videoItem.videoSize.height;
  291. CGPoint offset = CGPointMake((1.0 - scaleY) / 2.0 * self.videoItem.videoSize.width, (1 - scaleY) / 2.0 * self.videoItem.videoSize.height);
  292. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(scaleY, 0, 0, scaleY, -offset.x, -offset.y));
  293. }
  294. else if (self.contentMode == UIViewContentModeRight) {
  295. CGFloat scaleY = self.frame.size.height / self.videoItem.videoSize.height;
  296. CGPoint offset = CGPointMake(
  297. (1.0 - scaleY) / 2.0 * self.videoItem.videoSize.width,
  298. (1.0 - scaleY) / 2.0 * self.videoItem.videoSize.height);
  299. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(scaleY, 0, 0, scaleY, -offset.x + self.frame.size.width - self.videoItem.videoSize.width * scaleY, -offset.y));
  300. }
  301. else {
  302. CGFloat scaleX = self.frame.size.width / self.videoItem.videoSize.width;
  303. CGFloat scaleY = self.frame.size.height / self.videoItem.videoSize.height;
  304. CGPoint offset = CGPointMake((1.0 - scaleX) / 2.0 * self.videoItem.videoSize.width, (1 - scaleY) / 2.0 * self.videoItem.videoSize.height);
  305. self.drawLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake(scaleX, 0, 0, scaleY, -offset.x, -offset.y));
  306. }
  307. }
  308. - (void)layoutSubviews {
  309. [super layoutSubviews];
  310. [self resize];
  311. }
  312. - (void)update {
  313. [CATransaction setDisableActions:YES];
  314. for (SVGAContentLayer *layer in self.contentLayers) {
  315. if ([layer isKindOfClass:[SVGAContentLayer class]]) {
  316. [layer stepToFrame:self.currentFrame];
  317. }
  318. }
  319. [CATransaction setDisableActions:NO];
  320. if (self.forwardAnimating && self.audioLayers.count > 0) {
  321. for (SVGAAudioLayer *layer in self.audioLayers) {
  322. if (!layer.audioPlaying && layer.audioItem.startFrame <= self.currentFrame && self.currentFrame <= layer.audioItem.endFrame) {
  323. [layer.audioPlayer setCurrentTime:(NSTimeInterval)(layer.audioItem.startTime / 1000)];
  324. [layer.audioPlayer play];
  325. layer.audioPlaying = YES;
  326. }
  327. if (layer.audioPlaying && layer.audioItem.endFrame <= self.currentFrame) {
  328. [layer.audioPlayer stop];
  329. layer.audioPlaying = NO;
  330. }
  331. }
  332. }
  333. }
  334. - (void)next {
  335. if (self.reversing) {
  336. self.currentFrame--;
  337. if (self.currentFrame < (NSInteger)MAX(0, self.currentRange.location)) {
  338. self.currentFrame = MIN(self.videoItem.frames - 1, self.currentRange.location + self.currentRange.length - 1);
  339. self.loopCount++;
  340. }
  341. }
  342. else {
  343. self.currentFrame++;
  344. if (self.currentFrame >= MIN(self.videoItem.frames, self.currentRange.location + self.currentRange.length)) {
  345. self.currentFrame = MAX(0, self.currentRange.location);
  346. [self clearAudios];
  347. self.loopCount++;
  348. }
  349. }
  350. if (self.loops > 0 && self.loopCount >= self.loops) {
  351. [self stopAnimation];
  352. if (!self.clearsAfterStop && [self.fillMode isEqualToString:@"Backward"]) {
  353. [self stepToFrame:MAX(0, self.currentRange.location) andPlay:NO];
  354. }
  355. else if (!self.clearsAfterStop && [self.fillMode isEqualToString:@"Forward"]) {
  356. [self stepToFrame:MIN(self.videoItem.frames - 1, self.currentRange.location + self.currentRange.length - 1) andPlay:NO];
  357. }
  358. id delegate = self.delegate;
  359. if (delegate != nil && [delegate respondsToSelector:@selector(svgaPlayerDidFinishedAnimation:)]) {
  360. [delegate svgaPlayerDidFinishedAnimation:self];
  361. }
  362. return;
  363. }
  364. [self update];
  365. id delegate = self.delegate;
  366. if (delegate != nil) {
  367. if ([delegate respondsToSelector:@selector(svgaPlayer:didAnimatedToFrame:)]) {
  368. [delegate svgaPlayer:self didAnimatedToFrame:self.currentFrame];
  369. } else if ([delegate respondsToSelector:@selector(svgaPlayerDidAnimatedToFrame:)]){
  370. [delegate svgaPlayerDidAnimatedToFrame:self.currentFrame];
  371. }
  372. if (self.videoItem.frames > 0) {
  373. if ([delegate respondsToSelector:@selector(svgaPlayer:didAnimatedToPercentage:)]) {
  374. [delegate svgaPlayer:self didAnimatedToPercentage:(CGFloat)(self.currentFrame + 1) / (CGFloat)self.videoItem.frames];
  375. } else if ([delegate respondsToSelector:@selector(svgaPlayerDidAnimatedToPercentage:)]) {
  376. [delegate svgaPlayerDidAnimatedToPercentage:(CGFloat)(self.currentFrame + 1) / (CGFloat)self.videoItem.frames];
  377. }
  378. }
  379. }
  380. }
  381. - (void)setVideoItem:(SVGAVideoEntity *)videoItem {
  382. _videoItem = videoItem;
  383. _currentRange = NSMakeRange(0, videoItem.frames);
  384. _reversing = NO;
  385. _currentFrame = 0;
  386. _loopCount = 0;
  387. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  388. [self clear];
  389. [self draw];
  390. }];
  391. }
  392. #pragma mark - Dynamic Object
  393. - (void)setImage:(UIImage *)image forKey:(NSString *)aKey {
  394. if (image == nil) {
  395. return;
  396. }
  397. NSMutableDictionary *mutableDynamicObjects = [self.dynamicObjects mutableCopy];
  398. [mutableDynamicObjects setObject:image forKey:aKey];
  399. self.dynamicObjects = mutableDynamicObjects;
  400. if (self.contentLayers.count > 0) {
  401. for (SVGAContentLayer *layer in self.contentLayers) {
  402. if ([layer isKindOfClass:[SVGAContentLayer class]] && [layer.imageKey isEqualToString:aKey]) {
  403. layer.bitmapLayer.contents = (__bridge id _Nullable)([image CGImage]);
  404. }
  405. }
  406. }
  407. }
  408. - (void)setImageWithURL:(NSURL *)URL forKey:(NSString *)aKey {
  409. [[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  410. if (error == nil && data != nil) {
  411. UIImage *image = [UIImage imageWithData:data];
  412. if (image != nil) {
  413. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  414. [self setImage:image forKey:aKey];
  415. }];
  416. }
  417. }
  418. }] resume];
  419. }
  420. - (void)setImage:(UIImage *)image forKey:(NSString *)aKey referenceLayer:(CALayer *)referenceLayer {
  421. [self setImage:image forKey:aKey];
  422. }
  423. - (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString *)aKey {
  424. if (attributedText == nil) {
  425. return;
  426. }
  427. NSMutableDictionary *mutableDynamicTexts = [self.dynamicTexts mutableCopy];
  428. [mutableDynamicTexts setObject:attributedText forKey:aKey];
  429. self.dynamicTexts = mutableDynamicTexts;
  430. if (self.contentLayers.count > 0) {
  431. CGSize bitmapSize = CGSizeMake(self.videoItem.images[aKey].size.width * self.videoItem.images[aKey].scale, self.videoItem.images[aKey].size.height * self.videoItem.images[aKey].scale);
  432. CGSize size = [attributedText boundingRectWithSize:bitmapSize
  433. options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
  434. CATextLayer *textLayer;
  435. for (SVGAContentLayer *layer in self.contentLayers) {
  436. if ([layer isKindOfClass:[SVGAContentLayer class]] && [layer.imageKey isEqualToString:aKey]) {
  437. textLayer = layer.textLayer;
  438. if (textLayer == nil) {
  439. textLayer = [CATextLayer layer];
  440. [layer addSublayer:textLayer];
  441. layer.textLayer = textLayer;
  442. [layer resetTextLayerProperties:attributedText];
  443. }
  444. }
  445. }
  446. if (textLayer != nil) {
  447. textLayer.contentsScale = [[UIScreen mainScreen] scale];
  448. [textLayer setString:attributedText];
  449. textLayer.frame = CGRectMake(0, 0, size.width, size.height);
  450. }
  451. }
  452. }
  453. - (void)setDrawingBlock:(SVGAPlayerDynamicDrawingBlock)drawingBlock forKey:(NSString *)aKey {
  454. NSMutableDictionary *mutableDynamicDrawings = [self.dynamicDrawings mutableCopy];
  455. [mutableDynamicDrawings setObject:drawingBlock forKey:aKey];
  456. self.dynamicDrawings = mutableDynamicDrawings;
  457. if (self.contentLayers.count > 0) {
  458. for (SVGAContentLayer *layer in self.contentLayers) {
  459. if ([layer isKindOfClass:[SVGAContentLayer class]] &&
  460. [layer.imageKey isEqualToString:aKey]) {
  461. layer.dynamicDrawingBlock = drawingBlock;
  462. }
  463. }
  464. }
  465. }
  466. - (void)setHidden:(BOOL)hidden forKey:(NSString *)aKey {
  467. NSMutableDictionary *mutableDynamicHiddens = [self.dynamicHiddens mutableCopy];
  468. [mutableDynamicHiddens setObject:@(hidden) forKey:aKey];
  469. self.dynamicHiddens = mutableDynamicHiddens;
  470. if (self.contentLayers.count > 0) {
  471. for (SVGAContentLayer *layer in self.contentLayers) {
  472. if ([layer isKindOfClass:[SVGAContentLayer class]] &&
  473. [layer.imageKey isEqualToString:aKey]) {
  474. layer.dynamicHidden = hidden;
  475. }
  476. }
  477. }
  478. }
  479. - (void)clearDynamicObjects {
  480. self.dynamicObjects = nil;
  481. self.dynamicTexts = nil;
  482. self.dynamicHiddens = nil;
  483. self.dynamicDrawings = nil;
  484. }
  485. - (NSDictionary *)dynamicObjects {
  486. if (_dynamicObjects == nil) {
  487. _dynamicObjects = @{};
  488. }
  489. return _dynamicObjects;
  490. }
  491. - (NSDictionary *)dynamicTexts {
  492. if (_dynamicTexts == nil) {
  493. _dynamicTexts = @{};
  494. }
  495. return _dynamicTexts;
  496. }
  497. - (NSDictionary *)dynamicHiddens {
  498. if (_dynamicHiddens == nil) {
  499. _dynamicHiddens = @{};
  500. }
  501. return _dynamicHiddens;
  502. }
  503. - (NSDictionary<NSString *,SVGAPlayerDynamicDrawingBlock> *)dynamicDrawings {
  504. if (_dynamicDrawings == nil) {
  505. _dynamicDrawings = @{};
  506. }
  507. return _dynamicDrawings;
  508. }
  509. - (NSRunLoopMode)mainRunLoopMode {
  510. if (!_mainRunLoopMode) {
  511. _mainRunLoopMode = NSRunLoopCommonModes;
  512. }
  513. return _mainRunLoopMode;
  514. }
  515. @end