我发现我的应用程序中所有iOS动画都停止工作。在iOS7中经常发生这种情况。
我有一个支持iOS 5、6和7的应用程序。最近我发现在iOS7中,应用程序中的所有iOS动画都停止工作了?
我发现我的应用程序中所有iOS动画都停止工作。在iOS7中经常发生这种情况。
我有一个支持iOS 5、6和7的应用程序。最近我发现在iOS7中,应用程序中的所有iOS动画都停止工作了?
在IOS 7中,如果一些主要的方法操作是在后台线程上执行,则动画会被禁用。
因此,您需要重新启用动画,例如(一种解决方法)
[UIView setAnimationsEnabled:YES];
也许这可以帮助。
最近在为背景线程上的视图大小计算布局时遇到了这个问题。通过交换 setAnimationsEnabled:
,我发现唯一禁用背景线程中的动画的时间是在 -[UIImageView setImage:]
中。
因为这个视图从未被渲染,而且图片更改对我的计算也不需要,所以我能够将这个测试包含在一个主线程调用中:
if ([NSThread isMainThread]) {
self.answerImageView.image = [UIImage imageNamed:imgName];
}
值得注意的是,在初始视图实例化中,我不会遇到这个问题,因为我已经在主线程中加载我的模板视图,以避免Xib加载问题。
其他问题可能更加复杂,但你应该能够想出类似的解决方法。这是我用来检测动画背景禁用的类别。
#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
更新
事实证明,UIWebView
在显示媒体元素时实际上会进行不安全的setAnimationsEnabled:
调用(rdar://20314684)。 如果您的应用程序允许任意网络内容,则使上述方法始终处于活动状态非常痛苦。 因此,我已经开始使用下面的方法,因为它可以让我在失败后打开和关闭断点并继续执行:
#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
使用此代码,您可以在SEViewAlertForUnsafeBackgroundCalls
上添加一个符号断点或仅将断点插入函数体中来停止应用程序。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//make calculations
dispatch_async(dispatch_get_main_queue(),
^{
[UIView setAnimationsEnabled:YES];
});
});
它似乎解决了问题。