I am experiencing my application hangs when the user taps the home button:
The snippet causing the problem is this:
[UIView animateWithDuration:0.5
delay: (float)random()/RAND_MAX * 1.0f
options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
[self setAlpha:1.0f];
}
completion:^(BOOL finished){
}];
I believe what is happening is that the random delay schedules the animation, the user then quits – so the method that removes the animation (it is set to autorepeat) is never called? and the app hangs, while still showing the animation.
It is so bad that a restart of the iPhone is needed. I had similar issues with blocks and animations before so this was the first code I out-commented and the problem went away.
I have tried calling [self.layer remove allAnimations] on the view upon tapping the home button, but it does not help.
I have not been successful debugging the issue as it happens in “no-mans-land” after applicationWillEnterbackground has been called. Instruments shows nothing out of the ordinary.
Can someone offer help regarding what approach I should take to make sure animations with delays does not cause this behavior?
(it might be performSelector: withDelay behind the scenes):
Sending a removeAllAnimations call to the view’s layer should remove the animation.
You said “I have tried calling [self.layer remove allAnimations] on the view upon tapping the home button, but it does not help.”
You would not call self on the view, you would send that message to the view’s layer yourself.
From the view controller, the code might look something like this:
BTW, where does the code that you posted reside? It would need be run from an instance method of the view that you are animating in order to work.