从 Apple 的拖放 API 中,我只使用 Drag API。我屏幕上加载的所有图像都来自一组图像。当我尝试拖动图像时,一旦完成拖动图像,我就可以看到预览移动了。在控制台上打印以下行“PBItemCollectionServicer 连接已断开”。
下面是我的代码:
func freestyleImages(images: [UIImage]) {
if images.count > 0{
for image in images {
let randomX = CGFloat.random(in: 10..<50)
let randomY = CGFloat.random(in: 10..<50)
let imageView = UIImageView(image: image)
self.view.addSubview(imageView)
imageView.isUserInteractionEnabled = true
imageView.frame = CGRect(x: randomX, y: randomY, width: image.size.width, height: image.size.height)
}
}
}
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
let touchedPoint = session.location(in: self.view)
if let touchedImageView = self.view.hitTest(touchedPoint, with: nil) as? UIImageView {
let touchedImage = touchedImageView.image
let itemProvider = NSItemProvider(object: touchedImage!)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = touchedImageView
print(dragItem)
return [dragItem]
}
return []
}
func dragInteraction(_ interaction: UIDragInteraction, session: UIDragSession, didEndWith operation: UIDropOperation) {
print(operation.rawValue)
}
func dragInteraction(_ interaction: UIDragInteraction, previewForLifting item: UIDragItem, session: UIDragSession) -> UITargetedDragPreview? {
return UITargetedDragPreview(view: item.localObject as! UIView)
}
任何帮助将非常感激