11

I am seeing that all ios animations in my app stop working.Its happening very frequently in iOS7.

I had an app which is supporting iOS 5, 6 and 7. I am seeing recently that all iOS animations stop working in the app in iOS7?

2
  • I don't know the reason behind it but I had exactly same issue and it was resolved when I removed a tableView:willDisplayCell:forRowAtIndexPath: method which used to call lots of performSelectorInBackground to display images. This may help or may give some hint to solve the problem.
    – Mohammad
    Oct 18, 2013 at 7:38
  • I actually found out.It happens when you do UIKit stuff in the background thread.Here is the link it explains everything. stackoverflow.com/questions/18281097/… Oct 18, 2013 at 8:29

3 Answers 3

24

In IOS 7 when some main method action is performed on background thread then the animations get disabled.

so for this you need to re-enable the animations like (A workaround)

[UIView setAnimationsEnabled:YES];

May be this can help.

1
  • 3
    I am marking it as right although this is a work around.No main method action should be done in background thread. Jan 7, 2014 at 8:34
16

I ran into this problem recently with some views I am laying out for size calculations on the background thread. By swizzling setAnimationsEnabled: I found that the only time I was disabling animations from the background thread was in -[UIImageView setImage:].

Because this view was never rendered and image changes weren't required for my calculation, I was able to enclose this test in a main thread call:

if ([NSThread isMainThread]) {
    self.answerImageView.image = [UIImage imageNamed:imgName];
}

It's worth noting I don't hit this issue in the initial view instantiation because I already load my template views in the main thread to avoid a Xib loading issue.

Other issues may be more complex but you should be able to come up with similar workarounds. Here's the category I use to detect background disabling of animations.

#import <UIKit/UIKit.h>
#import <JRSwizzle/JRSwizzle.h>

#ifdef DEBUG

@implementation UIView (BadBackgroundBehavior)

+ (void)load
{
    NSError *error = nil;
    if (![self jr_swizzleClassMethod:@selector(setAnimationsEnabled:) withClassMethod:@selector(SE_setAnimationsEnabled:) error:&error]) {
        NSLog(@"Error! %@", error);
    }
}

+ (void)SE_setAnimationsEnabled:(BOOL)enabled
{
    NSAssert([NSThread isMainThread], @"This method is not thread safe. Look at the backtrace and decide if you really need to be doing this here.");
    [self SE_setAnimationsEnabled:enabled];
}

@end

#endif

Update

It turns out that UIWebView actually makes unsafe calls to setAnimationsEnabled: when displaying a media element (rdar://20314684). This makes the above method very painful to have active all the time if your app allows arbitrary web content. Instead I've started using the below method as it lets me turn the breakpoint on and off and continue after failure:

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

#ifdef DEBUG

void SEViewAlertForUnsafeBackgroundCalls() {
    NSLog(@"----------------------------------------------------------------------------------");
    NSLog(@"Background call to setAnimationsEnabled: detected. This method is not thread safe.");
    NSLog(@"Set a breakpoint at SEUIViewDidSetAnimationsOffMainThread to inspect this call.");
    NSLog(@"----------------------------------------------------------------------------------");
}

@implementation UIView (BadBackgroundBehavior)

+ (void)load
{
    method_exchangeImplementations(class_getInstanceMethod(object_getClass(self), @selector(setAnimationsEnabled:)),
                                   class_getInstanceMethod(object_getClass(self), @selector(SE_setAnimationsEnabled:)));
}

+ (void)SE_setAnimationsEnabled:(BOOL)enabled
{
    if (![NSThread isMainThread]) {
        SEViewAlertForUnsafeBackgroundCalls();
    }
    [self SE_setAnimationsEnabled:enabled];
}

@end

#endif

With this code, you can stop your app by adding a symbolic breakpoint on SEViewAlertForUnsafeBackgroundCalls or just sticking a breakpoint in the function body.

Gist

2
  • This answer is amazing. Whenever that assertion is thrown, you'll see in your source code where you've messed up and tried to do something not on the main thread. Feb 5, 2015 at 15:13
  • I was experiencing this behavior because of a webview that kept making unsafe calls to setAnimationsEnabled: and run into race conditions with other views animating. Is there a way to fix this webview nonsense? Adopting WKWebView is not an option because the webview is encapsulated in a third party class. Would permanently swizzling setAnimationsEnabled: to force it to run in the main be too dangerous? Jul 20, 2015 at 10:07
1

Extending Vinay's solution, that's what I do:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //make calculations
            dispatch_async(dispatch_get_main_queue(),
                           ^{
                                [UIView setAnimationsEnabled:YES];
                           });           
});

It seems to solve the problem.

2
  • this is the worst answer that I was expecting :D
    – l0gg3r
    Jul 15, 2014 at 13:29
  • what's the outside dispatch async for? Can't you just remove it?
    – AntiMoron
    Dec 30, 2016 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.