拖放 - 检测我的模型类型标识符

时间:2018-01-24 09:24:01

标签: ios swift drag-and-drop

我在我的drag and drop中实施UICollectionViewController,其{a}}数据源为custom Model我为测试目的而创建

Model如下:

class UserClass: NSObject, NSItemProviderWriting, NSItemProviderReading {
    static var readableTypeIdentifiersForItemProvider: [String] { return [] }

    static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
        return self.init(someData: "test")
    }

    static var writableTypeIdentifiersForItemProvider: [String] { return [] }

    func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {
        return nil
    }

    required init(someData:String) {}
}

这是我的UICollectionViewDragDelegate

// This is the cv datasource
var myModel: [UserClass] = [userClass1, userClass2, userClass3, userClass4, ...]

// UICollectionViewDragDelegate

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
    let item = myModel[indexPath.item]

    let itemProvider = NSItemProvider(object: item)
    let dragItem = UIDragItem(itemProvider: itemProvider)
    dragItem.localObject = item

    return [dragItem]
}

// To add multiple items for the drag session
func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
    let item = myModel[indexPath.item]
    let itemProvider = NSItemProvider(object: item)
    let dragItem = UIDragItem(itemProvider: itemProvider)
    dragItem.localObject = item

    return [dragItem]
}

以下是处理/接收丢弃的custom UIView

class CustomView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)            
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CustomView: UIDropInteractionDelegate {
    func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
        return session.hasItemsConforming(toTypeIdentifiers: [""]) // <-- Which type is my model type ? 
    }

    func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
        let dropLocation = session.location(in: self)
        let operation: UIDropOperation

        if self.frame.contains(dropLocation) {
            operation = session.localDragSession == nil ? .copy : .move
        } else {
            operation = .cancel
        }


        let dropProposal = UIDropProposal(operation: operation)
        dropProposal.isPrecise = true
        return dropProposal
    }

    func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
        print("drop detected\n")

        session.loadObjects(ofClass: UserClass.self) { (objects) in
            objects.forEach({ (object) in
                print("object description:", object)
            })
        }
    }
}

拖拽本身按预期工作,一个或多个项目响应拖拽会话并被解除但不知何故我的自定义视图没有检测到掉落,其功能都没有触发。

我认为可能存在的问题是,我不知道应该在哪里设置session.hasItemsConforming(tyTypeIdentifiers:[]),因为我不知道我的模型是哪种类型。

我可以参与其中吗?

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式决定哪种对象可以处理拖动:

  1. 所有对象:

    func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
       return true
    }
    
  2. 仅接受CustomView类型的对象:

    func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
       return session.canLoadObjects(ofClass: CustomView.self)
    } 
    
  3. 我猜后者更适合你的情况。