gpt4 book ai didi

ios - Core Animation 在应用重新出现时停止动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:14 24 4
gpt4 key购买 nike

我已经使用 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 的标志设置为 NO 来解决这个问题。不幸的是,在我的情况下它不起作用。有谁知道解决方案吗?提前致谢。

最佳答案

好的,我已经测试了您的代码并找到了修复方法。在您的应用委托(delegate)中添加以下内容:

- (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 来跟踪动画是否打开并注册通知(在您的 View 类中):

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

关于ios - Core Animation 在应用重新出现时停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28110606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com