这个问题与这个帖子有关:how-to-set-the-height-of-a-cell-depending-on-a-uilabel-with-a-typewriter-effect
在我的tableViewController中,我的单元格包含UILabel,其打字效果由 setTextWithTypeAnimation 管理;
func configureCell(tableView: UITableView, cell: ParagraphTableViewCell, atIndexPath indexPath: IndexPath) {
let paragraph = paragraphArray[indexPath.row] as! Paragraph
cell.paragraph = paragraph
self.queue = OperationQueue()
let operation1 = BlockOperation(block: {
cell.dialogueLabel.setTextWithTypeAnimation(typedText: paragraph.dialogueLabel.text!, queue:self.queue, callBackAfterCharacterInsertion: {
self.tableView.beginUpdates()
self.tableView.endUpdates()
})
})
operation1.completionBlock = {
cell.buttonsStackViewHeightConstraint.constant = CGFloat(HEIGHT_CONSTRAINT)
UIView.animate(withDuration: 0.3, animations: {
cell.contentView.layoutIfNeeded()
}, completion: nil)
}
queue.addOperation(operation1)
}
我的打字机位于UILabel扩展程序中:
extension UILabel {
func setTextWithTypeAnimation(typedText: String, queue: OperationQueue, characterInterval: TimeInterval = 0.05, callBackAfterCharacterInsertion:(()->())?) {
text = ""
for (_, character) in typedText.characters.enumerated() {
if queue.isSuspended {
OperationQueue.main.isSuspended = true
OperationQueue.main.cancelAllOperations()
break;
}
OperationQueue.main.addOperation {
self.text = self.text! + String(character)
callBackAfterCharacterInsertion?()
}
Thread.sleep(forTimeInterval: characterInterval)
}
}
}
首先我使用DispatchQueue来管理单元格内的动画(参见how-to-set-the-height-of-a-cell-depending-on-a-uilabel-with-a-typewriter-effect),但是当用户关闭视图控制器时我需要停止该线程。这就是我使用OperationQueue(DispatchQueue无法停止)
的原因@IBAction func closeViewController(sender: AnyObject) {
dismiss(animated: true, completion: nil)
if self.queue != nil {
self.queue.isSuspended = true
self.queue.cancelAllOperations()
self.queue = nil
}
}
问题是当调用completionBlock时,当我尝试更新布局约束时应用程序崩溃。
This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread.
如何避免此次崩溃?
答案 0 :(得分:2)
您必须致电UI
上的mainQueue
内容。
示例:
operation1.completionBlock = {
DispatchQueue.main.async {
cell.buttonsStackViewHeightConstraint.constant = CGFloat(HEIGHT_CONSTRAINT)
UIView.animate(withDuration: 0.3, animations: {
cell.contentView.layoutIfNeeded()
}, completion: nil)
}
}