Reputation: 1253
I have created animation to my menu using CABasicAnimation. Here below is my code relevant to this issue:
- (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];
}
When I call this view first time the animation works just fine. When this app is re-appeared from phone suspension the animation also works fine. But when I switch to another app or in the other word leave this app, then re-enter this app the animation is frozen. I found one article talking about this. They claimed it could be solved by setting a flag on the CABasicAnimation class called removedOnCompletion to NO. Unfortunately in my case it does not work. Does anyone know a solution to that? Thanks in advance.
Upvotes: 2
Views: 645
Reputation: 5616
Ok I've tested your code and found a fix. In your app delegate add the following :
- (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.
}
Then add a bool to track if the animation is on and register the notification (in your view class) :
BOOL animationActive = NO;
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPulseFilterSubView) name:@"resumeAnimation" object:nil];
[self setAwesomeMenu];
}
Then at the start of addPulseFilterSubView check if we need to stop the previous animation :
- (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]];
...
Then at the very end of this method :
animationActive = YES;
}
And all should be good :D
Upvotes: 1