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

Ios 拖放-检测我的模型类型标识符,ios,swift,drag-and-drop,Ios,Swift,Drag And Drop,我正在我的UICollectionViewController中实现拖放,它的数据源是我为测试目的创建的自定义模型 模型如下所示: class UserClass: NSObject, NSItemProviderWriting, NSItemProviderReading { static var readableTypeIdentifiersForItemProvider: [String] { return [] } static func object(withItem

我正在我的
UICollectionViewController
中实现
拖放
,它的数据源是我为测试目的创建的
自定义模型

模型
如下所示:

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]
}
下面是一个
自定义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)
            })
        }
    }
}
类自定义视图:UIView{
重写初始化(帧:CGRect){
super.init(frame:frame)
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}
扩展CustomView:UIDropInteractionDelegate{
func-dropInteraction(uinteraction:UIDropInteraction,canHandle session:UIDropSession)->Bool{
return session.hasItemsConforming(toTypeIdentifiers:[“”])//UIDropProposal{
让dropLocation=session.location(in:self)
let操作:UIDropOperation
如果self.frame.contains(dropLocation){
操作=session.localDragSession==nil?.copy:.move
}否则{
操作=.cancel
}
让dropProposal=UIDropProposal(操作:操作)
dropProposal.isPrecise=true
退货建议
}
func dropInteraction(uInteraction:UIDropInteraction,performDrop会话:UIDropSession){
打印(“检测到掉落\n”)
中的session.loadObjects(of类:UserClass.self){(对象)
forEach({(object)在
打印(“对象描述:”,对象)
})
}
}
}
拖动本身按预期工作,一个或多个项目响应拖动会话并被提升,但不知何故,我的自定义视图没有检测到下降,它的任何功能都没有启动

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


我能帮忙吗?

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

  • 所有对象:

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

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