能够缩短UIDragInteraction的长按时间

时间:2018-01-09 19:49:08

标签: ios swift drag-and-drop uidragitem

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

我意识到一个不直观的因素是UIDragInteraction需要长按至少一秒才能工作。我想知道是否有办法缩短长按时间? docs on 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 [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
            }
        }
    }
}

2 个答案:

答案 0 :(得分:5)

没有明显的方法可以做到这一点,但我只是遇到了同样的问题,并且看了一下拖拉交互附加到视图的手势识别器。它是_UIDragLiftGestureRecognizer,它不是公共API的一部分,但事实证明这只是UILongPressGestureRecognizer的子类。

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

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

答案 1 :(得分:1)

我试图从Xamarin.iOS的UIView内部实现IUIDragInteractionDelegate界面中执行此操作。在其构造函数中,我制作了一个SetupDragNDrop方法,该方法允许拖动视图而没有默认的延迟/延迟来捕获视图。我将代码放在下面,以防其他人使用:

    #region Private Fields
    private UIDragInteraction _UIDragInteraction;
    #endregion

    void Initialize()
    {
        SetupDragNDrop();
    }

    private void SetupDragNDrop()
    {
        UserInteractionEnabled = true;
        _UIDragInteraction = new UIDragInteraction(this);
        AddInteraction(_UIDragInteraction);

        // On iPad, this defaults to true. On iPhone, this defaults to 
        // false. Since this app should work on the iPhone, enable the the 
        // drag interaction.
        _UIDragInteraction.Enabled = true;

        SetupDragDelay();
    }

    private void SetupDragDelay()
    {

        UILongPressGestureRecognizer longPressGesture = new UILongPressGestureRecognizer();

        GestureRecognizers?.ToList().ForEach(gesture =>
        {
            var x = gesture as UILongPressGestureRecognizer;
            if (x != null)
            {
                longPressGesture = x;
            }
        });

        longPressGesture.MinimumPressDuration = 0.0;
    }