第一次發帖。對Swift和iOS開發比較陌生。逐漸掌握了它,但一直在嘗試新的東西,可以使用一些幫助來除錯以下問題。預先感謝!
問題
問題:
我有一個在自定義 UIView 中運行的紙屑影片,我將該 UIView 添加為點擊按鈕時的一個子視圖。我還擁有一個表視圖,我希望其單元格可以通過拖放來重新排序。為了做到這一點,我實作了各種UITableViewDragDelegate和UITableViewDropDelegate方法。
單獨來看,這兩個功能都在按預期作業。
然而,我遇到的一個不可預見的副作用是,在表視圖單元格被重新排序后,紙屑影片停止出現,直到應用程式被關閉并重新加載。無論紙屑影片視圖是否被添加到相同或不同的視圖控制器上,這種情況都是真實的。
一些觀察結果: UPDATE: 以下是我正在使用的相關拖放委托方法: 我的紙屑影片被呼叫如下: 我很感謝任何關于解決方案的想法或需要調查的事情。再次感謝! uj5u.com熱心網友回復: 奇怪的是,我可以按照你的步驟使用你的示例應用程式,并重現這個錯誤,但如果我在ConfettiView.swift的第98行設定一個斷點 程式在斷點處停止,而當我繼續運行時,錯誤消失了:彩紙表現正常。所以,如果我把這一行替換成:
問題就消失了。
標籤:
Thread.isMainThread在被呼叫時回傳True。
itemsForBeginning運行時,影片仍然按預期運行。當dropSessionDidUpdate運行時,影片停止出現。
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = self.journals[sourceIndexPath.row]
self.journals.remove(at: sourceIndexPath.row)
self.journals.insert(item, at: destinationIndexPath.row)
}
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
return [UIDragItem( itemProvider: NSItemProvider()) ]
}
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath DestinationIndexPath: IndexPath?) -> UITableViewDropProposal {
if session.localDragSession != nil { // Drag originated from the same app
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
return UITableViewDropProposal(operation: .cancel, intent: .unspecified)
}
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
//此方法未被呼叫,但需要符合UITableViewDropDelegate協議。 }
}
let confettiView = ConfettiView()
view.insertSubview(confettiView, belowSubview: reportsTable)
confettiView.emit()
emit()函式配置了CAEmitterCell、CAEmitterLayer和其他一些與影片相關的邏輯。我暫時沒有包括那段代碼,因為只要單元格沒有被重新排序,它就可以作業,但是如果有人認為問題可能在于影片的生成方式,我很樂意這樣做。
addBirthrateAnimation(to: confettiLayer)
DispatchQueue.main.asyncAfter(deadline: .now() 0.14) {
addBirthrateAnimation(to: confettiLayer)
}
