Ability to shorten UIDragInteraction's long press timing

后端 未结 2 985
渐次进展
渐次进展 2021-01-05 23:17

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

相关标签:
2条回答
  • 2021-01-06 00:02

    I was trying to do this from Xamarin.iOS inside an UIView implementing the IUIDragInteractionDelegate interface. In its constructor I made a SetupDragNDrop method that allows to drag the view without that default delay/latency to catch the view. I leave the code down below in case it's useful for somebody else:

        #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;
        }
    
    0 讨论(0)
  • 2021-01-06 00:10

    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.

    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
    }
    
    0 讨论(0)
提交回复
热议问题