【问题标题】:iOS 11 Drag and Drop UIDocumentiOS 11 拖放 UIDocument
【发布时间】:2018-02-06 22:31:16
【问题描述】:

除了如何处理 UIDocuments 之外,关于拖放的一切都很清楚。

这是我的(不工作的)实现...

对于拖动:

func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {

    let doc = DataManager.shared.storage[indexPath.row] // the UIDocument to drag
    let itemProv = NSItemProvider()
    itemProv.registerFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension",
                                        fileOptions: [.openInPlace],
                                        visibility: .all) { completionHandler in

        completionHandler(doc.fileURL, true, nil)
        return nil
    }

    // DragItem
    let item = UIDragItem(itemProvider: itemProv)

    return [item]
}

对于掉落:

func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {

    for item in coordinator.session.items {
        item.itemProvider.loadFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension", completionHandler: { (url, error) in

            if let docUrlOk = url {
                debugPrint(urlOk.absoluteString)
                DataManager.shared.load(docUrlOk)
            }
        })
    }
}

以及更新方法:

func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {

    if tableView.hasActiveDrag {
        if session.items.count > 1 {
            return UITableViewDropProposal(operation: .cancel)
        } else {
            return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
        }
    } else {
        return UITableViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
    }
}

非常感谢

【问题讨论】:

    标签: swift drag-and-drop uidocument


    【解决方案1】:

    在与 Apple 工程师多次交谈后,我找到了一个“半”解决方案。问题中发布的代码是正确的,但是要将专有文件从您的应用程序拖动到文件应用程序,您必须在 Target -> Info -> Exported UTIs 字段中插入以下值

    但是,如果您将专有文件从 Files App 拖回您的 App,则 UIKit 中存在一个错误,导致其无法正常工作......工程师告诉我等待 iOS 11.3 或 iOS 12,我们会看到

    【讨论】:

    • 更新 iOS 12:现在一切正常(您可以将专有文件从 Files App 拖到您的 App 中)。
    【解决方案2】:

    如果你在 iPhone 上运行代码,你需要设置 dragItem.enable = YES

    【讨论】:

    • 什么是dragItem?这是一个 UIDragItem 实例。如果是,则没有 .enable 属性....
    • 对不起,我的意思是 UIDragInteraction 的 ebable 属性
    • 已启用:tableView.dragDelegate = self - tableView.dropDelegate = self - tableView.dragInteractionEnabled = true
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多