iOS: add animation callback for iOS

This commit is contained in:
pengfei.zhou 2021-12-17 16:52:54 +08:00 committed by osborn
parent 96e61e2268
commit 2d1cf3fcfe

View File

@ -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<NSKeyValueChangeKey, id> *)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];
@ -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