• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIDropInteractionDelegate performDrop 没有被调用?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:15:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试构建一个简单的测试应用程序来学习拖放 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-推荐答案


就我而言,这是一个编程错误,我写了以下内容:

func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {

    return UIDropProposal(operation: .move)
}

但你不能总是使用move!仅当 session.allowsMo​​veOperationtrue!

来自 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap