Core Animation 在应用重新出现时停止动画
Core Animation stops animation on app re-appearance
我使用 CABasicAnimation 为我的菜单创建了动画。以下是我与此问题相关的代码:
- (void) viewDidLoad {
...
[self setAwesomeMenu];
}
- (void)setAwesomeMenu {
...
//set tag for awesomemenu subview
[menu setTag:awesomeMenuSubViewTag];
[self addPulseFilterSubView];
}
- (void) addPulseFilterSubView {
// add pulseEffectFilter subview
UIImageView *pulseEffectFilter = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pulse.png"]];
[self.view insertSubview:pulseEffectFilter belowSubview:[self.view viewWithTag:awesomeMenuSubViewTag]];
pulseEffectFilter.center = CGPointMake(160.0, 205.0);
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
theAnimation.duration=2.0;
theAnimation.repeatCount=HUGE_VALL;
//theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
theAnimation.toValue=[NSNumber numberWithFloat:1.3];
[pulseEffectFilter.layer addAnimation:theAnimation forKey:@"transform.scale"];
theAnimation.removedOnCompletion = NO; // doesn't help
CABasicAnimation *fadeOutAnimation;
fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOutAnimation.fromValue = [NSNumber numberWithFloat:1.2];
fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0];
fadeOutAnimation.duration = 1.0;
fadeOutAnimation.repeatCount=HUGE_VALL;
[pulseEffectFilter.layer addAnimation:fadeOutAnimation forKey:@"opacity"];
//fadeOutAnimation.autoreverses = YES;
fadeOutAnimation.removedOnCompletion = NO; //doesn't help
[pulseEffectFilter setTag:pulseEffectFilterSubViewTag];
}
当我第一次调用此视图时,动画效果很好。当此应用程序从 phone 暂停状态重新出现时,动画也可以正常工作。 但是当我切换到另一个应用程序或换句话说离开这个应用程序,然后重新进入这个应用程序时动画被冻结。我发现一个 article 在谈论这个。他们声称可以通过在 CABasicAnimation class 上设置一个名为 removedOnCompletion 的标志来解决这个问题。不幸的是,在我的情况下它不起作用。有谁知道解决方案吗?提前致谢。
好的,我已经测试了您的代码并找到了修复方法。在您的应用委托中添加以下内容:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:@"resumeAnimation" object:nil];
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
然后添加一个 bool 来跟踪动画是否打开并注册通知(在您看来 class):
BOOL animationActive = NO;
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPulseFilterSubView) name:@"resumeAnimation" object:nil];
[self setAwesomeMenu];
}
然后在 addPulseFilterSubView 开始时检查我们是否需要停止之前的动画:
- (void) addPulseFilterSubView {
if (animationActive) {
for (UIView * view in self.view.subviews) {
if (view.tag ==pulseEffectFilterSubViewTag) {
[view removeFromSuperview];
}
}
}
// add pulseEffectFilter subview
UIImageView *pulseEffectFilter = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pulse.png"]];
[self.view insertSubview:pulseEffectFilter belowSubview:[self.view viewWithTag:awesomeMenuSubViewTag]];
...
然后在这个方法的最后:
animationActive = YES;
}
一切都应该很好 :D
我使用 CABasicAnimation 为我的菜单创建了动画。以下是我与此问题相关的代码:
- (void) viewDidLoad {
...
[self setAwesomeMenu];
}
- (void)setAwesomeMenu {
...
//set tag for awesomemenu subview
[menu setTag:awesomeMenuSubViewTag];
[self addPulseFilterSubView];
}
- (void) addPulseFilterSubView {
// add pulseEffectFilter subview
UIImageView *pulseEffectFilter = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pulse.png"]];
[self.view insertSubview:pulseEffectFilter belowSubview:[self.view viewWithTag:awesomeMenuSubViewTag]];
pulseEffectFilter.center = CGPointMake(160.0, 205.0);
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
theAnimation.duration=2.0;
theAnimation.repeatCount=HUGE_VALL;
//theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
theAnimation.toValue=[NSNumber numberWithFloat:1.3];
[pulseEffectFilter.layer addAnimation:theAnimation forKey:@"transform.scale"];
theAnimation.removedOnCompletion = NO; // doesn't help
CABasicAnimation *fadeOutAnimation;
fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOutAnimation.fromValue = [NSNumber numberWithFloat:1.2];
fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0];
fadeOutAnimation.duration = 1.0;
fadeOutAnimation.repeatCount=HUGE_VALL;
[pulseEffectFilter.layer addAnimation:fadeOutAnimation forKey:@"opacity"];
//fadeOutAnimation.autoreverses = YES;
fadeOutAnimation.removedOnCompletion = NO; //doesn't help
[pulseEffectFilter setTag:pulseEffectFilterSubViewTag];
}
当我第一次调用此视图时,动画效果很好。当此应用程序从 phone 暂停状态重新出现时,动画也可以正常工作。 但是当我切换到另一个应用程序或换句话说离开这个应用程序,然后重新进入这个应用程序时动画被冻结。我发现一个 article 在谈论这个。他们声称可以通过在 CABasicAnimation class 上设置一个名为 removedOnCompletion 的标志来解决这个问题。不幸的是,在我的情况下它不起作用。有谁知道解决方案吗?提前致谢。
好的,我已经测试了您的代码并找到了修复方法。在您的应用委托中添加以下内容:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:@"resumeAnimation" object:nil];
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
然后添加一个 bool 来跟踪动画是否打开并注册通知(在您看来 class):
BOOL animationActive = NO;
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPulseFilterSubView) name:@"resumeAnimation" object:nil];
[self setAwesomeMenu];
}
然后在 addPulseFilterSubView 开始时检查我们是否需要停止之前的动画:
- (void) addPulseFilterSubView {
if (animationActive) {
for (UIView * view in self.view.subviews) {
if (view.tag ==pulseEffectFilterSubViewTag) {
[view removeFromSuperview];
}
}
}
// add pulseEffectFilter subview
UIImageView *pulseEffectFilter = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pulse.png"]];
[self.view insertSubview:pulseEffectFilter belowSubview:[self.view viewWithTag:awesomeMenuSubViewTag]];
...
然后在这个方法的最后:
animationActive = YES;
}
一切都应该很好 :D