[英]IOS get notified when UI updates.
I have a custom keyboard extension. 我有自定义键盘扩展名。 This function is called when the delete key is pressed:
按下删除键时调用此函数:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
{
for _ in 1..<50
{
(self.textDocumentProxy as UIKeyInput).deleteBackward()
}
print("Deletion End")
self.deleteCounter = 0
})
I do not think the dispatch_async
is relevent but I included it, just incase. 我不认为
dispatch_async
是相关的,但我把它包括在内,只是因为它。
The problem is that even though my console prints "Deletion End" once the loop is finished, the UI of the textfield does not update until a second or two has passed. 问题是,即使我的控制台在循环结束后打印“Deletion End”,文本字段的UI也不会更新,直到一两秒钟过去。
It seems calling 好像在呼唤
(self.textDocumentProxy as UIKeyInput).deleteBackward()
Does not immediately delete a character and update the UI. 不立即删除字符并更新UI。
How can I be notified when the UI is actually updated? 如何在实际更新UI时通知我?
Change like this: 改变如下:
dispatch_async(dispatch_get_main_queue(),{
for _ in 1..<50
{
(self.textDocumentProxy as UIKeyInput).deleteBackward()
}
print("Deletion End")
self.deleteCounter = 0
})
Explanation: 说明:
The UI must work in the Main thread so when you work with the background queue you always have to dispatch in main queue the UI updates. UI必须在主线程中工作,因此当您使用后台队列时,您始终必须在主队列中分派UI更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.