Skip to content

Commit 188c1b3

Browse files
committed
feat: support muti mask.
1 parent efa4dd9 commit 188c1b3

File tree

6 files changed

+51
-13
lines changed

6 files changed

+51
-13
lines changed

SVGAPlayer.xcodeproj/project.pbxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
63712E6521787950001AE014 /* heartbeat.svga in Resources */ = {isa = PBXBuildFile; fileRef = 63712E6421787950001AE014 /* heartbeat.svga */; };
1212
63712E6821787A45001AE014 /* SVGAAudioEntity.m in Sources */ = {isa = PBXBuildFile; fileRef = 63712E6721787A45001AE014 /* SVGAAudioEntity.m */; };
1313
63E817012178809D001D2D62 /* SVGAAudioLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E817002178809D001D2D62 /* SVGAAudioLayer.m */; };
14+
71418C712259FAA30029C69E /* mutiMask.svga in Resources */ = {isa = PBXBuildFile; fileRef = 71418C702259FAA30029C69E /* mutiMask.svga */; };
1415
80D4C7254846B96B9C6EED83 /* libPods-SVGAPlayer.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DDA5FF396660C7C932DF9B8 /* libPods-SVGAPlayer.a */; };
1516
904D41F81D223DD20085A21A /* SVGABezierPath.m in Sources */ = {isa = PBXBuildFile; fileRef = 904D41F71D223DD20085A21A /* SVGABezierPath.m */; };
1617
9052FC631E6EB8D4007BC925 /* SVGAExporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 9052FC621E6EB8D4007BC925 /* SVGAExporter.m */; };
@@ -63,6 +64,7 @@
6364
63712E6721787A45001AE014 /* SVGAAudioEntity.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAAudioEntity.m; sourceTree = "<group>"; };
6465
63E816FF2178809D001D2D62 /* SVGAAudioLayer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SVGAAudioLayer.h; sourceTree = "<group>"; };
6566
63E817002178809D001D2D62 /* SVGAAudioLayer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAAudioLayer.m; sourceTree = "<group>"; };
67+
71418C702259FAA30029C69E /* mutiMask.svga */ = {isa = PBXFileReference; lastKnownFileType = file; path = mutiMask.svga; sourceTree = "<group>"; };
6668
8AD65028FA2D122A34DC4A63 /* Pods-SVGAPlayer.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SVGAPlayer.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SVGAPlayer/Pods-SVGAPlayer.debug.xcconfig"; sourceTree = "<group>"; };
6769
904D41F61D223DD20085A21A /* SVGABezierPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVGABezierPath.h; sourceTree = "<group>"; };
6870
904D41F71D223DD20085A21A /* SVGABezierPath.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGABezierPath.m; sourceTree = "<group>"; };
@@ -239,6 +241,7 @@
239241
90D7C9FA1F7E2AA3006E74F0 /* Samples */ = {
240242
isa = PBXGroup;
241243
children = (
244+
71418C702259FAA30029C69E /* mutiMask.svga */,
242245
63712E6421787950001AE014 /* heartbeat.svga */,
243246
90D7CA1A1F7FB114006E74F0 /* rose_1.5.0.svga */,
244247
90D7CA191F7FB114006E74F0 /* rose_2.0.0.svga */,
@@ -327,6 +330,7 @@
327330
developmentRegion = English;
328331
hasScannedForEncodings = 0;
329332
knownRegions = (
333+
English,
330334
en,
331335
Base,
332336
);
@@ -350,6 +354,7 @@
350354
90A676ED1D13A6DF008A69F3 /* Assets.xcassets in Resources */,
351355
63712E6521787950001AE014 /* heartbeat.svga in Resources */,
352356
90A676EB1D13A6DF008A69F3 /* Main.storyboard in Resources */,
357+
71418C712259FAA30029C69E /* mutiMask.svga in Resources */,
353358
90CB64F91EF297E800DAA382 /* SVGAPlayer React-Info.plist in Resources */,
354359
90D7CA1C1F7FB114006E74F0 /* rose_1.5.0.svga in Resources */,
355360
90D7CA1B1F7FB114006E74F0 /* rose_2.0.0.svga in Resources */,

SVGAPlayer/Samples/mutiMask.svga

4.19 KB
Binary file not shown.

SVGAPlayer/ViewController.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ - (void)viewDidLoad {
2929
self.aPlayer.loops = 0;
3030
self.aPlayer.clearsAfterStop = YES;
3131
parser = [[SVGAParser alloc] init];
32-
[self onChange:nil];
32+
[parser parseWithNamed:@"mutiMask"
33+
inBundle:[NSBundle mainBundle] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
34+
if (videoItem != nil) {
35+
self.aPlayer.videoItem = videoItem;
36+
[self.aPlayer startAnimation];
37+
}
38+
} failureBlock:^(NSError * _Nonnull error) {
39+
}];
3340
}
3441

3542
- (void)viewWillLayoutSubviews {

Source/SVGAPlayer.m

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

Source/SVGAVideoSpriteEntity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
@interface SVGAVideoSpriteEntity : NSObject
1616

1717
@property (nonatomic, readonly) NSString *imageKey;
18+
@property (nonatomic, readonly) NSString *maskImageKey;
1819
@property (nonatomic, readonly) NSArray<SVGAVideoSpriteFrameEntity *> *frames;
1920

2021
- (instancetype)initWithJSONObject:(NSDictionary *)JSONObject;

Source/SVGAVideoSpriteEntity.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ - (instancetype)initWithJSONObject:(NSDictionary *)JSONObject {
2020
if (self) {
2121
if ([JSONObject isKindOfClass:[NSDictionary class]]) {
2222
NSString *imageKey = JSONObject[@"imageKey"];
23+
NSString *maskImageKey = JSONObject[@"maskImageKey"];
2324
NSArray<NSDictionary *> *JSONFrames = JSONObject[@"frames"];
2425
if ([imageKey isKindOfClass:[NSString class]] && [JSONFrames isKindOfClass:[NSArray class]]) {
2526
NSMutableArray<SVGAVideoSpriteFrameEntity *> *frames = [[NSMutableArray alloc] init];
@@ -28,6 +29,7 @@ - (instancetype)initWithJSONObject:(NSDictionary *)JSONObject {
2829
[frames addObject:[[SVGAVideoSpriteFrameEntity alloc] initWithJSONObject:obj]];
2930
}
3031
}];
32+
_maskImageKey = maskImageKey;
3133
_imageKey = imageKey;
3234
_frames = frames;
3335
}

0 commit comments

Comments
 (0)