- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我有一个如下所示的协议(protocol):
@protocol CryptoDelegate <NSObject>
- (void)cryptoManagerFinishedEncryption;
@end
有一个名为 CryptoManager
的单例,它具有以下功能:
- (void)startEncryption:(NSURL *)path withDelegate:(id<CryptoDelegate>)delegate {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// ... some stuff is happening here
[delegate cryptoManagerFinishedEncryption];
});
}
然后我有一个自制的 MultiPopup
可以显示面板(它必须扩展 MultiPopupPanel
来存储 i.a. 指向弹出窗口本身的指针)并在这些面板之间切换使用以下代码:
- (void)switchTo:(MultiPopupPanel*)panel {
if(self.currentPanel != panel) {
MultiPopupPanel* oldPanel = self.currentPanel;
self.currentPanel = panel;
self.currentPanel.alpha = 0;
self.currentPanel.hidden = NO;
if(oldPanel) {
NSLog(@"Before animation");
[UIView animateWithDuration:0.2f animations:^{
oldPanel.alpha = 0;
} completion:^(BOOL finished) {
NSLog(@"After animation");
oldPanel.hidden = YES;
[UIView animateWithDuration:0.2f animations:^{
self.currentPanel.alpha = 1;
}];
}];
} else {
[UIView animateWithDuration:0.2f animations:^{
self.currentPanel.alpha = 1;
}];
}
}
}
现在我有一个扩展 MultiPopupPanel
并实现协议(protocol) CryptoDelegate
的面板。在其 cryptoManagerFinishedEncryption
实现中,我正在调用我的 switchTo
函数,该函数随后应切换到另一个面板。
这是应用程序挂起的地方:立即输出消息“Before animation”。然后应用挂起 15-20 秒,然后出现动画,输出“After animation”消息并显示新面板。
只有当我在另一个线程中(通过 dispatch_async)使用 +[UIView animateWithDuration...]
时才会发生这种情况。如果我在没有 dispatch_async
(= 在主线程中)的情况下调用委托(delegate),一切都会顺利。
为什么会这样?是否允许从异步 block 调用动画?非常感谢任何帮助。
最佳答案
您必须始终在主线程上调用 UI 相关代码。不这样做是被禁止的,并且会导致意想不到的结果(通常是异常(exception))。
当调用您的委托(delegate)方法时,您应该再次将 dispatch_async
与 dispatch_get_main_queue
一起使用,这样委托(delegate)代码将在主线程上运行,您可以更新您的 UI。
另见 these Apple docs了解更多具体信息。
关于ios - UIView animateWithDuration 使应用程序在从 dispatch_async block 调用时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27766897/
我如何在 dispatch_async 中正确调用 dispatch_async 调用 dispatch_group_t downloadQueue = dispatch_group_create()
我想按顺序添加一个 dispatch_async,但我不希望它们随机启动。我想举个例子: dispatch_async 1 开始... dispatch_async 1 结束。 dispatch_as
我正在尝试从 Firebase 下载一些带有图像的帖子并将其显示在我的表格 View 中。在下面的代码中,我注意到 tableview 仅在从 preLoadImage 函数调用下载每个图像后才加载。
我正在尝试学习 GCD,所以我还没有完全掌握它的工作原理。出于某种原因,我在调用以下方法后遇到帧率永久下降的情况。如果我不使用调度函数而只是在主循环中写入数据,帧速率将保持在 60。我不知道为什么。
我在将 JSON 数据存储到单个对象数组中时遇到问题。看起来问题出在处理 JSON 请求的 dispatch_asynch 的执行中。当我在方法之前创建一个断点而不是单步执行应用程序时,它似乎只是通过
我知道 dispatch_async 可以处理线程。 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
我有一个使用dispatch_async的函数: - (IBAction)action:(id)sender { int i=10000; NSString * data; d
我猜不到它的输出。 dispatch_async(serial_queue,^{NSlog(@"1");}); NSlog(@"2"); dispatch_async(serial_queue,^{N
NSString *firstID = @"https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg"; 想象一
dispatch_queue_t queue = dispatch_queue_create("setup_cell", NULL); dispatch_async(queue, ^{ //L
我正在使用 here 的解决方案使 titleView clipsToBounds 始终为真。 我的 ViewController 中有这个并且运行良好,但是,如果我通过按后退按钮离开 ViewCon
我正在开发一个应用程序,我必须在其中从 JSON 获取数据并在 UITableView 中显示。正在后台获取数据。但它似乎进入了无限循环。 任何帮助将不胜感激。 - (UITableViewCell
我是 Swift 的新手,正在研究 dispatch_async 函数的工作原理。 API 文档显示 dispatch_async 有两个参数。但是,我可以传递一个参数,这没关系。 dispatch_
所以我运行了一段代码,我想提高性能,我注意到删除需要很长时间才能完成(大约 0.003 秒),所以我决定将它放入另一个线程,然后删除数组。 现在创建和运行线程所花费的时间比删除数组快得多,但是现在我创
我有以下代码... -(void) SetSerialNumber { NSLog(@"SetSerialNumber"); NSString *serialNum = textFie
假设我有以下代码: dispatch_async(dispatch_get_main_queue()) { myFunction() } 这表示异步调用调用 myFunction 的 bloc
我正在使用一些下载数据的代码。该代码使用 block 作为回调。有几种代码非常相似的下载方法:在回调 block 中,如果出现问题,它们会显示 UIAlertView。警报 View 始终如下所示:
我是 Swift 的新手,正在研究 dispatch_async 函数的工作原理。 API 文档显示 dispatch_async 有两个参数。但是,我可以传递一个参数,这没关系。 dispatch_
那么第一个问题就是dispatch_async是如何决定使用哪个线程的呢?只是随机选择它?我需要做一些解析和核心数据的事情,所以我不想阻塞 UI 线程并使用 dispatch_async,但在那之后我
假设我将任务异步分派(dispatch)到队列: { // we are on main queue dispatch_async(dispatch_get_global_queue(
我是一名优秀的程序员,十分优秀!