Skip to content

Commit 5776ef8

Browse files
committed
add clearCache.
1 parent 484d03a commit 5776ef8

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

SVGAPlayer/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ - (void)viewDidLoad {
2727
self.aPlayer.clearsAfterStop = YES;
2828
SVGAParser *parser = [[SVGAParser alloc] init];
2929

30-
[parser parseWithURL:[NSURL URLWithString:@"http://172.26.86.8:8080/movie.spec.zip"] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
30+
[parser parseWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://172.26.86.8:8080/movie.spec.zip?%u", arc4random()]] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
3131
if (videoItem != nil) {
3232
self.aPlayer.videoItem = videoItem;
3333
[self.aPlayer startAnimation];

Source/SVGAParser.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ - (void)parseWithURL:(nonnull NSURL *)URL
2626
completionBlock(videoItem);
2727
}
2828
} failureBlock:^(NSError * _Nonnull error) {
29+
[self clearCache:[self cacheKey:URL]];
2930
if (failureBlock) {
3031
failureBlock(error);
3132
}
@@ -39,6 +40,7 @@ - (void)parseWithURL:(nonnull NSURL *)URL
3940
completionBlock(videoItem);
4041
}
4142
} failureBlock:^(NSError * _Nonnull error) {
43+
[self clearCache:[self cacheKey:URL]];
4244
if (failureBlock) {
4345
failureBlock(error);
4446
}
@@ -86,6 +88,11 @@ - (void)parseWithCacheKey:(nonnull NSString *)cacheKey
8688
}];
8789
}
8890

91+
- (void)clearCache:(nonnull NSString *)cacheKey {
92+
NSString *cacheDir = [self cacheDirectory:cacheKey];
93+
[[NSFileManager defaultManager] removeItemAtPath:cacheDir error:NULL];
94+
}
95+
8996
- (void)parseWithData:(nonnull NSData *)data
9097
cacheKey:(nonnull NSString *)cacheKey
9198
completionBlock:(void ( ^ _Nullable)(SVGAVideoEntity * _Nonnull videoItem))completionBlock

Source/SVGAVectorLayer.m

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ - (void)createSublayers:(SVGAVectorLayer *)previous {
3838
else if ([shape[@"type"] isEqualToString:@"ellipse"]) {
3939
[self addSublayer:[self createEllipseLayer:shape]];
4040
}
41+
else if ([shape[@"type"] isEqualToString:@"rect"]) {
42+
[self addSublayer:[self createRectLayer:shape]];
43+
}
4144
else if ([shape[@"type"] isEqualToString:@"keep"] && previous != nil) {
4245
for (CALayer *item in previous.sublayers) {
4346
[self addSublayer:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:item]]];
@@ -57,6 +60,7 @@ - (CALayer *)createCurveLayer:(NSDictionary *)shape {
5760
}
5861
CAShapeLayer *shapeLayer = [bezierPath createLayer];
5962
[self resetStyles:shapeLayer shape:shape];
63+
[self resetTransform:shapeLayer shape:shape];
6064
return shapeLayer;
6165
}
6266

@@ -68,7 +72,7 @@ - (CALayer *)createEllipseLayer:(NSDictionary *)shape {
6872
[shape[@"args"][@"radiusX"] isKindOfClass:[NSNumber class]] &&
6973
[shape[@"args"][@"radiusY"] isKindOfClass:[NSNumber class]]) {
7074
CGFloat x = [shape[@"args"][@"x"] floatValue];
71-
CGFloat y = [shape[@"args"][@"x"] floatValue];
75+
CGFloat y = [shape[@"args"][@"y"] floatValue];
7276
CGFloat rx = [shape[@"args"][@"radiusX"] floatValue];
7377
CGFloat ry = [shape[@"args"][@"radiusY"] floatValue];
7478
bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x - rx, y - ry, rx * 2, ry * 2)];
@@ -78,6 +82,35 @@ - (CALayer *)createEllipseLayer:(NSDictionary *)shape {
7882
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
7983
[shapeLayer setPath:[bezierPath CGPath]];
8084
[self resetStyles:shapeLayer shape:shape];
85+
[self resetTransform:shapeLayer shape:shape];
86+
return shapeLayer;
87+
}
88+
else {
89+
return [CALayer layer];
90+
}
91+
}
92+
93+
- (CALayer *)createRectLayer:(NSDictionary *)shape {
94+
UIBezierPath *bezierPath;
95+
if ([shape[@"args"] isKindOfClass:[NSDictionary class]]) {
96+
if ([shape[@"args"][@"x"] isKindOfClass:[NSNumber class]] &&
97+
[shape[@"args"][@"y"] isKindOfClass:[NSNumber class]] &&
98+
[shape[@"args"][@"width"] isKindOfClass:[NSNumber class]] &&
99+
[shape[@"args"][@"height"] isKindOfClass:[NSNumber class]] &&
100+
[shape[@"args"][@"cornerRadius"] isKindOfClass:[NSNumber class]]) {
101+
CGFloat x = [shape[@"args"][@"x"] floatValue];
102+
CGFloat y = [shape[@"args"][@"y"] floatValue];
103+
CGFloat width = [shape[@"args"][@"width"] floatValue];
104+
CGFloat height = [shape[@"args"][@"height"] floatValue];
105+
CGFloat cornerRadius = [shape[@"args"][@"cornerRadius"] floatValue];
106+
bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x, y, width, height) cornerRadius:cornerRadius];
107+
}
108+
}
109+
if (bezierPath != nil) {
110+
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
111+
[shapeLayer setPath:[bezierPath CGPath]];
112+
[self resetStyles:shapeLayer shape:shape];
113+
[self resetTransform:shapeLayer shape:shape];
81114
return shapeLayer;
82115
}
83116
else {
@@ -120,4 +153,23 @@ - (void)resetStyles:(CAShapeLayer *)shapeLayer shape:(NSDictionary *)shape {
120153
}
121154
}
122155

156+
- (void)resetTransform:(CAShapeLayer *)shapeLayer shape:(NSDictionary *)shape {
157+
if ([shape[@"transform"] isKindOfClass:[NSDictionary class]]) {
158+
if ([shape[@"transform"][@"a"] isKindOfClass:[NSNumber class]] &&
159+
[shape[@"transform"][@"b"] isKindOfClass:[NSNumber class]] &&
160+
[shape[@"transform"][@"c"] isKindOfClass:[NSNumber class]] &&
161+
[shape[@"transform"][@"d"] isKindOfClass:[NSNumber class]] &&
162+
[shape[@"transform"][@"tx"] isKindOfClass:[NSNumber class]] &&
163+
[shape[@"transform"][@"ty"] isKindOfClass:[NSNumber class]]) {
164+
shapeLayer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMake([shape[@"transform"][@"a"] floatValue],
165+
[shape[@"transform"][@"b"] floatValue],
166+
[shape[@"transform"][@"c"] floatValue],
167+
[shape[@"transform"][@"d"] floatValue],
168+
[shape[@"transform"][@"tx"] floatValue],
169+
[shape[@"transform"][@"ty"] floatValue])
170+
);
171+
}
172+
}
173+
}
174+
123175
@end

0 commit comments

Comments
 (0)