Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Fix bug that when view is not loaded or not attached to a window, the…
Browse files Browse the repository at this point in the history
… CA animation completion callback is immediately called.
  • Loading branch information
qianyuan.wqy committed Sep 11, 2019
1 parent 6daad9e commit d951f97
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions ios/sdk/WeexSDK/Sources/Module/WXAnimationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -355,28 +355,53 @@ - (void)transitionWithArgs:(NSDictionary *)args withProperty:(NSString *)propert

- (void)animation:(WXComponent *)targetComponent args:(NSDictionary *)args callback:(WXModuleKeepAliveCallback)callback
{
/* Check if view of targetComponent is created, if not, we do not do animation and
simulate delay of 'duration' and callback.
For a view in list, the view migth be recycled, and if view is not attached to a window,
the CATransaction completion block will be called immediately, which may cause CPU overload
problem if JS code do any logic in the completion callback.
*/

BOOL shouldDoAnimation = NO;
if ([targetComponent isViewLoaded]) {
UIView* view = targetComponent.view;
if ([view window] != nil) {
shouldDoAnimation = YES;
}
}

/**
UIView-style animation functions support the standard timing functions,
but they don’t allow you to specify your own cubic Bézier curve.
CATransaction can be used instead to force these animations to use the supplied CAMediaTimingFunction to pace animations.
**/
[CATransaction begin];
[CATransaction setAnimationTimingFunction:[WXConvert CAMediaTimingFunction:args[@"timingFunction"]]];
[CATransaction setCompletionBlock:^{
if (callback) {
NSDictionary *message;
if (_isAnimationedSuccess) {
message = @{@"result":@"Success",
@"message":@"Success"};
}
else
{
message = @{@"result":@"Fail",
@"message":@"Animation did not complete"};

if (shouldDoAnimation) {
[CATransaction setCompletionBlock:^{
if (callback) {
NSDictionary *message;
if (_isAnimationedSuccess) {
message = @{@"result":@"Success",
@"message":@"Success"};
}
else
{
message = @{@"result":@"Fail",
@"message":@"Animation did not complete"};
}
callback(message, NO);
}
callback(message,NO);
}
}];
}];
}
else if (callback) {
double duration = [[args objectForKey:@"duration"] doubleValue] / 1000.f;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
callback(@{@"result":@"Success",
@"message":@"Success"}, NO);
});
}

BOOL needLayout = NO;
WXTransition* transition = nil;
Expand All @@ -387,6 +412,7 @@ - (void)animation:(WXComponent *)targetComponent args:(NSDictionary *)args callb
}

[CATransaction commit];

if (needLayout && transition) {
WXPerformBlockOnComponentThread(^{
[transition _handleTransitionWithStyles:transitionDic resetStyles:nil target:targetComponent];
Expand Down

0 comments on commit d951f97

Please sign in to comment.