我有以下基于 block 的动画:
[UIView animateWithDuration:0.5f delay:0.0f
options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut
animations:^{
[view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)];
NSLog(@"animating");
}completion:^(BOOL finished){
NSLog(@"Completed");
}];
当应用程序从后台返回时,完成 block 被调用,我的动画不会重新启动。我尝试使用以下委托(delegate)方法重新启动动画:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
*/
[[self viewController] animate];
......
}
但这并不能恢复动画。
同样,我已经尝试了这些问题的答案中列出的方法:
但是那里的所有建议都对我没有用。当应用程序从后台返回时,是否有另一种方法可以恢复基于 block 的 UIView 动画?
最佳答案
一个 friend 解决了问题,需要在 View 从后台返回时在 View 上启用动画
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
*/
[UIView enableAnimations:YES];
[[self viewController] animate];
......
}
然后在 block 动画之前需要移除AllAnimations并将layer.transform设置为Identity
hasStarted = YES;
for(UIButton * button in goldenBreakOutButtons){
for (UIView* view in button.subviews) {
if (wasStarted) {
[view.layer removeAllAnimations];
view.layer.transform = CATransform3DIdentity;
}
if ([view isKindOfClass:[UIImageView class]]) {
[UIView animateWithDuration:0.5f delay:0.0f
options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat
animations:^ {
[view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)];
NSLog(@"animating");
}
completion:^(BOOL finished){
if (finished) {
NSLog(@"Completed");
}
}];
}
}
}
关于ios - 当应用程序进入前台时,如何重新启动基于 block 的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7670032/