@@ -35,6 +35,8 @@ @interface SVGAPlayer ()
3535
3636@implementation SVGAPlayer
3737
38+ static NSArray *_contentLayers;
39+
3840- (instancetype )initWithFrame : (CGRect)frame
3941{
4042 self = [super initWithFrame: frame];
@@ -55,6 +57,7 @@ - (void)startAnimation {
5557 [self stopAnimation: NO ];
5658 self.loopCount = 0 ;
5759 self.displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector (next )];
60+
5861 self.displayLink .frameInterval = 60 / self.videoItem .FPS ;
5962 [self .displayLink addToRunLoop: [NSRunLoop mainRunLoop ] forMode: NSRunLoopCommonModes ];
6063 self.forwardAnimating = !self.reversing ;
@@ -93,6 +96,7 @@ - (void)stopAnimation:(BOOL)clear {
9396}
9497
9598- (void )clear {
99+ _contentLayers = nil ;
96100 [self .drawLayer removeFromSuperlayer ];
97101}
98102
@@ -128,7 +132,9 @@ - (void)draw {
128132 self.drawLayer = [[CALayer alloc ] init ];
129133 self.drawLayer .frame = CGRectMake (0 , 0 , self.videoItem .videoSize .width , self.videoItem .videoSize .height );
130134 self.drawLayer .masksToBounds = true ;
131- [self .videoItem.sprites enumerateObjectsUsingBlock: ^(SVGAVideoSpriteEntity * _Nonnull sprite, NSUInteger idx, BOOL * _Nonnull stop) {
135+ NSMutableDictionary *tempHostLayers = [NSMutableDictionary dictionary ];
136+ NSMutableDictionary *tempContentLayers = [NSMutableDictionary dictionary ];
137+ for (SVGAVideoSpriteEntity * sprite in self.videoItem .sprites ) {
132138 UIImage *bitmap;
133139 if (sprite.imageKey != nil ) {
134140 if (self.dynamicObjects [sprite.imageKey] != nil ) {
@@ -140,7 +146,20 @@ - (void)draw {
140146 }
141147 SVGAContentLayer *contentLayer = [sprite requestLayerWithBitmap: bitmap];
142148 contentLayer.imageKey = sprite.imageKey ;
143- [self .drawLayer addSublayer: contentLayer];
149+ tempContentLayers[sprite.imageKey] = contentLayer;
150+ if ([sprite.imageKey containsString: @" .mask" ]) {
151+ CALayer *hostLayer = [[CALayer alloc ] init ];
152+ hostLayer.mask = contentLayer;
153+ [self .drawLayer addSublayer: hostLayer];
154+ tempHostLayers[sprite.imageKey] = hostLayer;
155+ } else {
156+ if (sprite.maskImageKey ) {
157+ CALayer *hostLayer = tempHostLayers[sprite.maskImageKey];
158+ [hostLayer addSublayer: contentLayer];
159+ } else {
160+ [self .drawLayer addSublayer: contentLayer];
161+ }
162+ }
144163 if (sprite.imageKey != nil ) {
145164 if (self.dynamicTexts [sprite.imageKey] != nil ) {
146165 NSAttributedString *text = self.dynamicTexts [sprite.imageKey];
@@ -160,7 +179,11 @@ - (void)draw {
160179 contentLayer.dynamicDrawingBlock = self.dynamicDrawings [sprite.imageKey];
161180 }
162181 }
163- }];
182+ }
183+ _contentLayers = tempContentLayers.allValues ;
184+ // [self.videoItem.sprites enumerateObjectsUsingBlock:^(SVGAVideoSpriteEntity * _Nonnull sprite, NSUInteger idx, BOOL * _Nonnull stop) {
185+
186+ // }];
164187 [self .layer addSublayer: self .drawLayer];
165188 NSMutableArray *audioLayers = [NSMutableArray array ];
166189 [self .videoItem.audios enumerateObjectsUsingBlock: ^(SVGAAudioEntity * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
@@ -252,7 +275,7 @@ - (void)layoutSubviews {
252275
253276- (void )update {
254277 [CATransaction setDisableActions: YES ];
255- for (SVGAContentLayer *layer in self. drawLayer . sublayers ) {
278+ for (SVGAContentLayer *layer in _contentLayers ) {
256279 if ([layer isKindOfClass: [SVGAContentLayer class ]]) {
257280 [layer stepToFrame: self .currentFrame];
258281 }
@@ -331,8 +354,8 @@ - (void)setImage:(UIImage *)image forKey:(NSString *)aKey {
331354 NSMutableDictionary *mutableDynamicObjects = [self .dynamicObjects mutableCopy ];
332355 [mutableDynamicObjects setObject: image forKey: aKey];
333356 self.dynamicObjects = mutableDynamicObjects;
334- if (self. drawLayer . sublayers .count > 0 ) {
335- for (SVGAContentLayer *layer in self. drawLayer . sublayers ) {
357+ if (_contentLayers .count > 0 ) {
358+ for (SVGAContentLayer *layer in _contentLayers ) {
336359 if ([layer isKindOfClass: [SVGAContentLayer class ]] && [layer.imageKey isEqualToString: aKey]) {
337360 layer.bitmapLayer .contents = (__bridge id _Nullable)([image CGImage ]);
338361 }
@@ -364,10 +387,10 @@ - (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString
364387 NSMutableDictionary *mutableDynamicTexts = [self .dynamicTexts mutableCopy ];
365388 [mutableDynamicTexts setObject: attributedText forKey: aKey];
366389 self.dynamicTexts = mutableDynamicTexts;
367- if (self. drawLayer . sublayers .count > 0 ) {
390+ if (_contentLayers .count > 0 ) {
368391 CGSize size = [attributedText boundingRectWithSize: CGSizeMake (CGFLOAT_MAX, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin context: NULL ].size ;
369392 CATextLayer *textLayer;
370- for (SVGAContentLayer *layer in self. drawLayer . sublayers ) {
393+ for (SVGAContentLayer *layer in _contentLayers ) {
371394 if ([layer isKindOfClass: [SVGAContentLayer class ]] && [layer.imageKey isEqualToString: aKey]) {
372395 textLayer = layer.textLayer ;
373396 if (textLayer == nil ) {
@@ -389,8 +412,8 @@ - (void)setDrawingBlock:(SVGAPlayerDynamicDrawingBlock)drawingBlock forKey:(NSSt
389412 NSMutableDictionary *mutableDynamicDrawings = [self .dynamicDrawings mutableCopy ];
390413 [mutableDynamicDrawings setObject: drawingBlock forKey: aKey];
391414 self.dynamicDrawings = mutableDynamicDrawings;
392- if (self. drawLayer . sublayers .count > 0 ) {
393- for (SVGAContentLayer *layer in self. drawLayer . sublayers ) {
415+ if (_contentLayers .count > 0 ) {
416+ for (SVGAContentLayer *layer in _contentLayers ) {
394417 if ([layer isKindOfClass: [SVGAContentLayer class ]] &&
395418 [layer.imageKey isEqualToString: aKey]) {
396419 layer.dynamicDrawingBlock = drawingBlock;
@@ -403,8 +426,8 @@ - (void)setHidden:(BOOL)hidden forKey:(NSString *)aKey {
403426 NSMutableDictionary *mutableDynamicHiddens = [self .dynamicHiddens mutableCopy ];
404427 [mutableDynamicHiddens setObject: @(hidden) forKey: aKey];
405428 self.dynamicHiddens = mutableDynamicHiddens;
406- if (self. drawLayer . sublayers .count > 0 ) {
407- for (SVGAContentLayer *layer in self. drawLayer . sublayers ) {
429+ if (_contentLayers .count > 0 ) {
430+ for (SVGAContentLayer *layer in _contentLayers ) {
408431 if ([layer isKindOfClass: [SVGAContentLayer class ]] &&
409432 [layer.imageKey isEqualToString: aKey]) {
410433 layer.dynamicHidden = hidden;
0 commit comments