从 iOS 9 开始,UICollectionView 通过beginInteractiveMovement及其同级方法支持交互式移动。从iOS 11开始,它还支持拖放,这似乎也支持移动。在 iOS 13 beta 4 中,拖放手势已与UIContextMenuInteraction动画相结合(Twitter 链接),因此您可以从上下文菜单过渡到拖动操作,但通过交互式移动 API 重新排序时情况并非如此。
在集合视图中使用拖放来实现项目的交互式移动是否更好?
让我们按部分去!
我正试图Drag and Drop在我的实施中实施UICollectionViewController.
该数据源UICollectionView是我创建array的自定义数据源Model Struct.
根据需要,我设置了我,collectionView.dragDelegate = self并通过这样做,我已经实现了required protocol function itemsForBeginning session: UIDragSession...
这是我的问题开始的地方:
struct Model {
// some variables
// Some initializations
}
var myModelDatasource: [Model] = [model1, model2, model3, ...] // it's a simple case example
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let item = myModelDatasource[indexPath.row]
let itemProvider = NSItemProvider(object: item)
let dragItem = UIDragItem(itemProvider: itemProvider) // …Run Code Online (Sandbox Code Playgroud) 我有一个UIImageView,通过添加它使其可拖动,UIDragInteraction如下面的代码所示。现在,我可以向任何方向拖动它,但是,我希望将可拖动项目限制在 y 轴(垂直)。即沿 x 轴没有运动。我怎样才能做到这一点?
let dragInteraction = UIDragInteraction(delegate: self)
dragInteraction.isEnabled = true
dragView.addInteraction(dragInteraction)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将Drag and Drop实现到我共享图像的应用程序中.
我的所有图像都是高性能缩略图(即小尺寸),因此我不能将它们用作我UIDragItem的图像,至少不能用作最终图像.
我正在寻找的是一种方法,为我的原始图像提供URL并将其发送为UIDragItem,然后使目标异步获取图像.当图像存储在iCloud中时,这在照片应用程序中完成,所以它必须以某种方式可能,我似乎无法弄清楚如何.
我当前正在使用iOS 11 UIDragInteraction并UIDropInteraction使其可用,以提供简单的拖放功能,用户可以在其中将UIImageView拖动到UIView上。
我意识到一个不直观的元素是,这UIDragInteraction需要长按至少一秒钟才能起作用。我想知道是否有办法缩短长按时间?Apple上的文档似乎并未强调这一点。
谢谢!
粘贴以下实现以供参考:
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
@IBOutlet var dropArea: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let dragInteraction = UIDragInteraction(delegate: self)
imageView.addInteraction(dragInteraction)
dragInteraction.isEnabled = true
let dropInteraction = UIDropInteraction(delegate: self)
dropArea.addInteraction(dropInteraction)
}
}
extension ViewController: UIDragInteractionDelegate {
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
guard let image = imageView.image
else { return [] }
let itemProvider = NSItemProvider(object: image)
return …Run Code Online (Sandbox Code Playgroud)