KingPolygon
KingPolygon

Reputation: 4755

UIButton Animation Stops When Returning to App?

I'm calling the following method in my menuViewController's viewDidLoad & viewDidAppear, to give my button a pulsing effect, when the app loads my main view.

-(void) pulseButton {
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = FLT_MAX;
[menuButton.layer addAnimation:pulseAnimation forKey:nil];
}

When I first load the app it pulses perfectly. Why does it stop animating when I leave the app and return immediately?

UPDATE

I added an NSLog to my viewDidAppear method and I realize it is not getting called. The view I'm displaying is a subview that I added in the viewDidLoad of my main controller the following way:

MenuViewController *MVC = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
[self.view addSubview:MVC.view];
[self addChildViewController:MVC];
[MVC didMoveToParentViewController:self];

Could that be the problem as to why viewDidAppear is not being called, hence my method isn't being called?

Upvotes: 0

Views: 151

Answers (2)

Yogi
Yogi

Reputation: 3588

Adding a view as a sub view will not call its didAppear method. You should do the stuff in viewDidLoad either.

Upvotes: 1

Himanshu Joshi
Himanshu Joshi

Reputation: 3399

You cam simply use NSNotificationCenter to achieve this

In your viewDidLoad

- (void)viewDidLoad
{
    //YOUR_STUFF

    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(pulseButton)
                                                name:UIApplicationDidBecomeActiveNotification
                                              object:nil];

}

** CALayer animation automatically removes when View is not visible

Upvotes: 1

Related Questions