我正在尝试构建一个简单的测试应用程序来学习拖放 API。对于这个问题,我只关注 Drop 场景。我有一个空白的 View Controller ,打开了 safari 应用程序(多任务处理),然后我尝试将图像从谷歌拖到 View Controller 的 View 上。
我可以将图像从 safari 拖到我应用的 View Controller 中,但是当我放手时,下面的这个调用永远不会被调用:
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession)
这是我的代码:
class EditTestVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.addInteraction(UIDropInteraction(delegate: self))
}
}
extension EditTestVC:UIDropInteractionDelegate {
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool { // 1
print("canHandle session: \(session)")
return true
// return session.canLoadObjects(ofClass: UIImage.self)
}
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal { // 2
print("sessionDidUpdate session: \(session)")
return UIDropProposal(operation: .copy)
}
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
print("performDrop session: \(session)")
}
}
上面的两个方法被调用,这个:
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool
还有这个:
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal
所以,我想弄清楚为什么会这样:
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession)
永远不会被调用。
有什么建议吗?
Best Answer-推荐答案 strong>
就我而言,这是一个编程错误,我写了以下内容:
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
return UIDropProposal(operation: .move)
}
但你不能总是使用move !仅当 session.allowsMoveOperation 为 true !
来自 move documentation :
You may use this operation only if the drop session's allowsMoveOperation property is true; otherwise, it's treated as a UIDropOperation.cancel operation. [...]
来自 cancel documentation :
If the user attempts a drop activity, the drag operation is canceled and the dropInteraction(_:performDrop delegate method is not called.
关于ios - UIDropInteractionDelegate performDrop 没有被调用?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/54817243/
|