ios - Core Animation 在应用重新出现时停止动画
<p><p>我使用 <strong>CABasicAnimation</strong> 为我的菜单创建了动画。下面是我与此问题相关的代码:</p>
<pre><code>- (void) viewDidLoad {
...
;
}
- (void)setAwesomeMenu {
...
//set tag for awesomemenu subview
;
;
}
- (void) addPulseFilterSubView {
// add pulseEffectFilter subview
UIImageView *pulseEffectFilter = [ initWithImage:];
];
pulseEffectFilter.center = CGPointMake(160.0, 205.0);
CABasicAnimation *theAnimation;
theAnimation=;
theAnimation.duration=2.0;
theAnimation.repeatCount=HUGE_VALL;
//theAnimation.autoreverses=YES;
theAnimation.fromValue=;
theAnimation.toValue=;
;
theAnimation.removedOnCompletion = NO; // doesn't help
CABasicAnimation *fadeOutAnimation;
fadeOutAnimation = ;
fadeOutAnimation.fromValue = ;
fadeOutAnimation.toValue = ;
fadeOutAnimation.duration = 1.0;
fadeOutAnimation.repeatCount=HUGE_VALL;
;
//fadeOutAnimation.autoreverses = YES;
fadeOutAnimation.removedOnCompletion = NO; //doesn't help
;
}
</code></pre>
<p>当我第一次调用这个 View 时,动画效果很好。当这个应用程序从手机暂停中重新出现时,动画也可以正常工作。 <strong>但是当我切换到另一个应用程序时,或者换句话说,离开这个应用程序,然后重新进入这个应用程序,动画就被卡住了。</strong>我找到了一个 <a href="http://richardwarrender.com/2011/09/core-animation-stops-ani-on-app-relaunch/" rel="noreferrer noopener nofollow">article</a>谈论这个。他们声称可以通过在 CABasicAnimation 类上设置一个名为 removedOnCompletion 的标志来解决这个问题。不幸的是,在我的情况下它不起作用。有谁知道解决方案?提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好的,我已经测试了您的代码并找到了修复方法。在您的应用委托(delegate)中添加以下内容:</p>
<pre><code>- (void)applicationDidBecomeActive:(UIApplication *)application {
[ 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.
}
</code></pre>
<p>然后添加一个 bool 值来跟踪动画是否开启并注册通知(在您的 View 类中):</p>
<pre><code>BOOL animationActive = NO;
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad {
[ addObserver:self selector:@selector(addPulseFilterSubView) name:@"resumeAnimation" object:nil];
;
}
</code></pre>
<p>然后在 addPulseFilterSubView 的开始检查我们是否需要停止之前的动画:</p>
<pre><code>- (void) addPulseFilterSubView {
if (animationActive) {
for (UIView * view in self.view.subviews) {
if (view.tag ==pulseEffectFilterSubViewTag) {
;
}
}
}
// add pulseEffectFilter subview
UIImageView *pulseEffectFilter = [ initWithImage:];
];
...
</code></pre>
<p>然后在这个方法的最后:</p>
<pre><code> animationActive = YES;
}
</code></pre>
<p>一切都应该很好:D</p></p>
<p style="font-size: 20px;">关于ios - Core Animation 在应用重新出现时停止动画,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28110606/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28110606/
</a>
</p>
页:
[1]