我在我的UICollectionViewController中实现了UICollectionViewController,它的数据源是我为测试目的创建的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:[]),因为我不知道我的模型是哪种类型。
能帮个忙吗?
发布于 2018-01-24 09:34:40
您可以通过以下方式决定哪种对象可以处理拖动:
CustomView类型的对象:
函数dropInteraction(_ interaction: UIDropInteraction,canHandle会话: UIDropSession) -> Bool (返回session.canLoadObjects(ofClass: CustomView.self) }我想后者更适合你的情况。
https://stackoverflow.com/questions/48418856
复制相似问题