本文介绍了能够缩短 UIDragInteraction 的长按时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 iOS 11 中提供的 UIDragInteractionUIDropInteraction 来实现简单的拖放功能,用户可以将 UIImageView 拖到 UIView 上.

I am currently using UIDragInteraction and UIDropInteraction made available in iOS 11 to make a simple drag and drop feature, where user could drag an UIImageView onto a UIView.

我意识到其中一个不直观的元素是 UIDragInteraction 需要长按至少一秒钟才能工作.我想知道是否有办法缩短长按时间? Apple 上的文档 似乎没有强调这一点.

I realized that one unintuitive element to this is that the UIDragInteraction requires a long press of at least a second to work. I was wondering if there is a way to shorten the long press duration? The docs on Apple doesn't seem to highlight this.

谢谢!

下面粘贴的实现供参考:

Implementation pasted below for reference:

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 [UIDragItem(itemProvider: itemProvider)]
    }
}

extension ViewController: UIDropInteractionDelegate {
    func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
        return UIDropProposal(operation: .copy)
    }

    func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
        guard let itemProvider = session.items.first?.itemProvider,
            itemProvider.canLoadObject(ofClass: UIImage.self)
            else { return }

        itemProvider.loadObject(ofClass: UIImage.self) { [weak self] loadedItem, error in
            guard let image = loadedItem as? UIImage
                else { return }

            DispatchQueue.main.async {
                self?.dropArea.image = image
            }
        }
    }
}

推荐答案

没有明显的方法可以做到这一点,但我只是面临同样的问题,并查看了 dragInteraction 附加到的视图的手势识别器.它是一个 _UIDragLiftGestureRecognizer,它不是公共 API 的一部分,但事实证明这只是 UILongPressGestureRecognizer 的子类.

There's no obvious way to do this, but I was just facing the same problem and took a peek into the gesture recognizers of the view that the dragInteraction is attached to. It a _UIDragLiftGestureRecognizer which is not part of the public API, but turns out this is just a subclass of UILongPressGestureRecognizer.

因此,在将您的 UIDragInteraction 添加到您的视图之后,并将该视图添加到视图层次结构之后(因为我使用的是自定义 UIView 子类,所以我只是将它添加到 didMoveToSuperview()),你可以这样做:

So, after having added your UIDragInteraction to your view, and after having added that view to the view hierachy (since I'm using a custom UIView subclass I just added it into didMoveToSuperview()), you can do something like this:

if let longPressRecognizer = gestureRecognizers?.compactMap({ $0 as? UILongPressGestureRecognizer}).first {
    longPressRecognizer.minimumPressDuration = 0.1 // your custom value
}

这篇关于能够缩短 UIDragInteraction 的长按时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:27