From 2d1cf3fcfe0bbf78e01ce594b29782cfd4bf198c Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 17 Dec 2021 16:52:54 +0800 Subject: [PATCH] iOS: add animation callback for iOS --- doric-iOS/Pod/Classes/Shader/DoricImageNode.m | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m index 7b4c9f3c..5a3521b3 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m @@ -38,7 +38,7 @@ @implementation DoricImageView - (void)displayLayer:(CALayer *)layer { if (@available(iOS 14.0, *)) { if ([self.image isKindOfClass:YYImage.class] - && ((YYImage *) self.image).animatedImageData) { + && ((YYImage *) self.image).animatedImageData) { [super displayLayer:layer]; } else { layer.contents = (__bridge id) self.image.CGImage; @@ -71,6 +71,7 @@ @implementation DoricImageView @interface DoricImageNode () @property(nonatomic, copy) NSString *loadCallbackId; +@property(nonatomic, copy) NSString *animationEndCallbackId; @property(nonatomic, assign) UIViewContentMode contentMode; @property(nonatomic, strong) NSNumber *placeHolderColor; @property(nonatomic, strong) NSString *placeHolderImage; @@ -555,11 +556,53 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id self.stretchInsetDic = (NSDictionary *) prop; } else if ([@"imageScale" isEqualToString:name]) { //Do not need set + } else if ([@"onAnimationEnd" isEqualToString:name]) { + self.animationEndCallbackId = prop; + DoricImageView *doricImageView = (DoricImageView *) view; +#if DORIC_USE_YYWEBIMAGE + [doricImageView addObserver:self + forKeyPath:@"currentIsPlayingAnimation" + options:NSKeyValueObservingOptionOld + context:nil]; +#elif DORIC_USE_SDWEBIMAGE + [doricImageView addObserver:self + forKeyPath:@"currentFrameIndex" + options:NSKeyValueObservingOptionNew + context:nil]; +#endif } else { [super blendView:view forPropName:name propValue:prop]; } } +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + DoricImageView *doricImageView = (DoricImageView *) self.view; +#if DORIC_USE_YYWEBIMAGE + if ([keyPath isEqualToString:@"currentIsPlayingAnimation"]) { + if (!self.animationEndCallbackId) { + return; + } + if (!doricImageView.currentIsPlayingAnimation + && [change[@"old"] boolValue]) { + [self callJSResponse:self.animationEndCallbackId, nil]; + } + } +#elif DORIC_USE_SDWEBIMAGE + if ([keyPath isEqualToString:@"currentFrameIndex"]) { + if (!self.animationEndCallbackId) { + return; + } + SDAnimatedImagePlayer *player = doricImageView.player; + if (player.totalLoopCount > 0 + && player.currentLoopCount == player.totalLoopCount - 1 + && player.currentFrameIndex == player.totalFrameCount - 1 + ) { + [self callJSResponse:self.animationEndCallbackId, nil]; + } + } +#endif +} + - (UIImage *)imageNamed:(NSString *)name { #if DORIC_USE_YYWEBIMAGE YYImage *image = [YYImage imageNamed:name]; @@ -611,7 +654,7 @@ - (NSNumber *)isAnimating { - (void)startAnimating { #if DORIC_USE_YYWEBIMAGE - [(DoricImageView *)self.view setCurrentAnimatedImageIndex:0]; + [(DoricImageView *) self.view setCurrentAnimatedImageIndex:0]; #endif [self.view startAnimating]; } @@ -645,4 +688,12 @@ - (BOOL)needReload { } return NO; } + +- (void)dealloc { +#if DORIC_USE_YYWEBIMAGE + [(DoricImageView *) self.view removeObserver:self forKeyPath:@"currentIsPlayingAnimation" context:nil]; +#elif DORIC_USE_SDWEBIMAGE + [(DoricImageView *) self.view removeObserver:self forKeyPath:@"currentFrameIndex" context:nil]; +#endif +} @end