我使用 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];
}
当我第一次调用这个 View 时,动画效果很好。当这个应用程序从手机暂停中重新出现时,动画也可以正常工作。 但是当我切换到另一个应用程序时,或者换句话说,离开这个应用程序,然后重新进入这个应用程序,动画就被卡住了。我找到了一个 article谈论这个。他们声称可以通过在 CABasicAnimation 类上设置一个名为 removedOnCompletion 的标志来解决这个问题。不幸的是,在我的情况下它不起作用。有谁知道解决方案?提前致谢。
好的,我已经测试了您的代码并找到了修复方法。在您的应用委托(delegate)中添加以下内容:
- (void)applicationDidBecomeActiveUIApplication *)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 值来跟踪动画是否开启并注册通知(在您的 View 类中):
BOOL animationActive = NO;
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(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;
}
一切都应该很好
关于ios - Core Animation 在应用重新出现时停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28110606/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |