核心动画在应用重新出现时停止动画

时间:2015-01-23 13:05:43

标签: ios objective-c cabasicanimation

我使用 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];
}

当我第一次调用此视图时,动画效果很好。当这个应用程序从手机暂停重新出现时,动画也可以正常工作。 但当我切换到另一个应用程序或在另一个单词中离开此应用程序,然后重新进入此应用程序时动画被冻结。我找到一个article谈论这个。他们声称可以通过将名为removedOnCompletion的CABasicAnimation类上的标志设置为NO来解决。不幸的是,在我的情况下它不起作用。有谁知道解决方案?提前谢谢。

1 个答案:

答案 0 :(得分:1)

好的我已经测试了你的代码并找到了修复程序。在您的app 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来跟踪动画是否打开并注册通知(在您的视图类中):

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