Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
feat: support muti mask.
Browse files Browse the repository at this point in the history
  • Loading branch information
errnull committed Apr 7, 2019
1 parent efa4dd9 commit 188c1b3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
5 changes: 5 additions & 0 deletions SVGAPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
63712E6521787950001AE014 /* heartbeat.svga in Resources */ = {isa = PBXBuildFile; fileRef = 63712E6421787950001AE014 /* heartbeat.svga */; };
63712E6821787A45001AE014 /* SVGAAudioEntity.m in Sources */ = {isa = PBXBuildFile; fileRef = 63712E6721787A45001AE014 /* SVGAAudioEntity.m */; };
63E817012178809D001D2D62 /* SVGAAudioLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E817002178809D001D2D62 /* SVGAAudioLayer.m */; };
71418C712259FAA30029C69E /* mutiMask.svga in Resources */ = {isa = PBXBuildFile; fileRef = 71418C702259FAA30029C69E /* mutiMask.svga */; };
80D4C7254846B96B9C6EED83 /* libPods-SVGAPlayer.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DDA5FF396660C7C932DF9B8 /* libPods-SVGAPlayer.a */; };
904D41F81D223DD20085A21A /* SVGABezierPath.m in Sources */ = {isa = PBXBuildFile; fileRef = 904D41F71D223DD20085A21A /* SVGABezierPath.m */; };
9052FC631E6EB8D4007BC925 /* SVGAExporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 9052FC621E6EB8D4007BC925 /* SVGAExporter.m */; };
Expand Down Expand Up @@ -63,6 +64,7 @@
63712E6721787A45001AE014 /* SVGAAudioEntity.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAAudioEntity.m; sourceTree = "<group>"; };
63E816FF2178809D001D2D62 /* SVGAAudioLayer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SVGAAudioLayer.h; sourceTree = "<group>"; };
63E817002178809D001D2D62 /* SVGAAudioLayer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SVGAAudioLayer.m; sourceTree = "<group>"; };
71418C702259FAA30029C69E /* mutiMask.svga */ = {isa = PBXFileReference; lastKnownFileType = file; path = mutiMask.svga; sourceTree = "<group>"; };
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>"; };
904D41F61D223DD20085A21A /* SVGABezierPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVGABezierPath.h; sourceTree = "<group>"; };
904D41F71D223DD20085A21A /* SVGABezierPath.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGABezierPath.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -239,6 +241,7 @@
90D7C9FA1F7E2AA3006E74F0 /* Samples */ = {
isa = PBXGroup;
children = (
71418C702259FAA30029C69E /* mutiMask.svga */,
63712E6421787950001AE014 /* heartbeat.svga */,
90D7CA1A1F7FB114006E74F0 /* rose_1.5.0.svga */,
90D7CA191F7FB114006E74F0 /* rose_2.0.0.svga */,
Expand Down Expand Up @@ -327,6 +330,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand All @@ -350,6 +354,7 @@
90A676ED1D13A6DF008A69F3 /* Assets.xcassets in Resources */,
63712E6521787950001AE014 /* heartbeat.svga in Resources */,
90A676EB1D13A6DF008A69F3 /* Main.storyboard in Resources */,
71418C712259FAA30029C69E /* mutiMask.svga in Resources */,
90CB64F91EF297E800DAA382 /* SVGAPlayer React-Info.plist in Resources */,
90D7CA1C1F7FB114006E74F0 /* rose_1.5.0.svga in Resources */,
90D7CA1B1F7FB114006E74F0 /* rose_2.0.0.svga in Resources */,
Expand Down
Binary file added SVGAPlayer/Samples/mutiMask.svga
Binary file not shown.
9 changes: 8 additions & 1 deletion SVGAPlayer/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ - (void)viewDidLoad {
self.aPlayer.loops = 0;
self.aPlayer.clearsAfterStop = YES;
parser = [[SVGAParser alloc] init];
[self onChange:nil];
[parser parseWithNamed:@"mutiMask"
inBundle:[NSBundle mainBundle] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
if (videoItem != nil) {
self.aPlayer.videoItem = videoItem;
[self.aPlayer startAnimation];
}
} failureBlock:^(NSError * _Nonnull error) {
}];
}

- (void)viewWillLayoutSubviews {
Expand Down
47 changes: 35 additions & 12 deletions Source/SVGAPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ @interface SVGAPlayer ()

@implementation SVGAPlayer

static NSArray *_contentLayers;

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
Expand All @@ -55,6 +57,7 @@ - (void)startAnimation {
[self stopAnimation:NO];
self.loopCount = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(next)];

self.displayLink.frameInterval = 60 / self.videoItem.FPS;
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
self.forwardAnimating = !self.reversing;
Expand Down Expand Up @@ -93,6 +96,7 @@ - (void)stopAnimation:(BOOL)clear {
}

- (void)clear {
_contentLayers = nil;
[self.drawLayer removeFromSuperlayer];
}

Expand Down Expand Up @@ -128,7 +132,9 @@ - (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) {
NSMutableDictionary *tempHostLayers = [NSMutableDictionary dictionary];
NSMutableDictionary *tempContentLayers = [NSMutableDictionary dictionary];
for (SVGAVideoSpriteEntity * sprite in self.videoItem.sprites) {
UIImage *bitmap;
if (sprite.imageKey != nil) {
if (self.dynamicObjects[sprite.imageKey] != nil) {
Expand All @@ -140,7 +146,20 @@ - (void)draw {
}
SVGAContentLayer *contentLayer = [sprite requestLayerWithBitmap:bitmap];
contentLayer.imageKey = sprite.imageKey;
[self.drawLayer addSublayer:contentLayer];
tempContentLayers[sprite.imageKey] = contentLayer;
if ([sprite.imageKey containsString:@".mask"]) {
CALayer *hostLayer = [[CALayer alloc] init];
hostLayer.mask = contentLayer;
[self.drawLayer addSublayer:hostLayer];
tempHostLayers[sprite.imageKey] = hostLayer;
} else {
if (sprite.maskImageKey) {
CALayer *hostLayer = tempHostLayers[sprite.maskImageKey];
[hostLayer addSublayer:contentLayer];
} else {
[self.drawLayer addSublayer:contentLayer];
}
}
if (sprite.imageKey != nil) {
if (self.dynamicTexts[sprite.imageKey] != nil) {
NSAttributedString *text = self.dynamicTexts[sprite.imageKey];
Expand All @@ -160,7 +179,11 @@ - (void)draw {
contentLayer.dynamicDrawingBlock = self.dynamicDrawings[sprite.imageKey];
}
}
}];
}
_contentLayers = tempContentLayers.allValues;
// [self.videoItem.sprites enumerateObjectsUsingBlock:^(SVGAVideoSpriteEntity * _Nonnull sprite, NSUInteger idx, BOOL * _Nonnull stop) {

// }];
[self.layer addSublayer:self.drawLayer];
NSMutableArray *audioLayers = [NSMutableArray array];
[self.videoItem.audios enumerateObjectsUsingBlock:^(SVGAAudioEntity * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
Expand Down Expand Up @@ -252,7 +275,7 @@ - (void)layoutSubviews {

- (void)update {
[CATransaction setDisableActions:YES];
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
for (SVGAContentLayer *layer in _contentLayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]]) {
[layer stepToFrame:self.currentFrame];
}
Expand Down Expand Up @@ -331,8 +354,8 @@ - (void)setImage:(UIImage *)image forKey:(NSString *)aKey {
NSMutableDictionary *mutableDynamicObjects = [self.dynamicObjects mutableCopy];
[mutableDynamicObjects setObject:image forKey:aKey];
self.dynamicObjects = mutableDynamicObjects;
if (self.drawLayer.sublayers.count > 0) {
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
if (_contentLayers.count > 0) {
for (SVGAContentLayer *layer in _contentLayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]] && [layer.imageKey isEqualToString:aKey]) {
layer.bitmapLayer.contents = (__bridge id _Nullable)([image CGImage]);
}
Expand Down Expand Up @@ -364,10 +387,10 @@ - (void)setAttributedText:(NSAttributedString *)attributedText forKey:(NSString
NSMutableDictionary *mutableDynamicTexts = [self.dynamicTexts mutableCopy];
[mutableDynamicTexts setObject:attributedText forKey:aKey];
self.dynamicTexts = mutableDynamicTexts;
if (self.drawLayer.sublayers.count > 0) {
if (_contentLayers.count > 0) {
CGSize size = [attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
CATextLayer *textLayer;
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
for (SVGAContentLayer *layer in _contentLayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]] && [layer.imageKey isEqualToString:aKey]) {
textLayer = layer.textLayer;
if (textLayer == nil) {
Expand All @@ -389,8 +412,8 @@ - (void)setDrawingBlock:(SVGAPlayerDynamicDrawingBlock)drawingBlock forKey:(NSSt
NSMutableDictionary *mutableDynamicDrawings = [self.dynamicDrawings mutableCopy];
[mutableDynamicDrawings setObject:drawingBlock forKey:aKey];
self.dynamicDrawings = mutableDynamicDrawings;
if (self.drawLayer.sublayers.count > 0) {
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
if (_contentLayers.count > 0) {
for (SVGAContentLayer *layer in _contentLayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]] &&
[layer.imageKey isEqualToString:aKey]) {
layer.dynamicDrawingBlock = drawingBlock;
Expand All @@ -403,8 +426,8 @@ - (void)setHidden:(BOOL)hidden forKey:(NSString *)aKey {
NSMutableDictionary *mutableDynamicHiddens = [self.dynamicHiddens mutableCopy];
[mutableDynamicHiddens setObject:@(hidden) forKey:aKey];
self.dynamicHiddens = mutableDynamicHiddens;
if (self.drawLayer.sublayers.count > 0) {
for (SVGAContentLayer *layer in self.drawLayer.sublayers) {
if (_contentLayers.count > 0) {
for (SVGAContentLayer *layer in _contentLayers) {
if ([layer isKindOfClass:[SVGAContentLayer class]] &&
[layer.imageKey isEqualToString:aKey]) {
layer.dynamicHidden = hidden;
Expand Down
1 change: 1 addition & 0 deletions Source/SVGAVideoSpriteEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@interface SVGAVideoSpriteEntity : NSObject

@property (nonatomic, readonly) NSString *imageKey;
@property (nonatomic, readonly) NSString *maskImageKey;
@property (nonatomic, readonly) NSArray<SVGAVideoSpriteFrameEntity *> *frames;

- (instancetype)initWithJSONObject:(NSDictionary *)JSONObject;
Expand Down
2 changes: 2 additions & 0 deletions Source/SVGAVideoSpriteEntity.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ - (instancetype)initWithJSONObject:(NSDictionary *)JSONObject {
if (self) {
if ([JSONObject isKindOfClass:[NSDictionary class]]) {
NSString *imageKey = JSONObject[@"imageKey"];
NSString *maskImageKey = JSONObject[@"maskImageKey"];
NSArray<NSDictionary *> *JSONFrames = JSONObject[@"frames"];
if ([imageKey isKindOfClass:[NSString class]] && [JSONFrames isKindOfClass:[NSArray class]]) {
NSMutableArray<SVGAVideoSpriteFrameEntity *> *frames = [[NSMutableArray alloc] init];
Expand All @@ -28,6 +29,7 @@ - (instancetype)initWithJSONObject:(NSDictionary *)JSONObject {
[frames addObject:[[SVGAVideoSpriteFrameEntity alloc] initWithJSONObject:obj]];
}
}];
_maskImageKey = maskImageKey;
_imageKey = imageKey;
_frames = frames;
}
Expand Down

0 comments on commit 188c1b3

Please sign in to comment.