void LoadRecognizers()
        {
            if (ElementGestureRecognizers == null)
            {
                return;
            }

#if __MOBILE__
            if (_shouldReceiveTouch == null)
            {
                // Cache this so we don't create a new UITouchEventArgs instance for every recognizer
                _shouldReceiveTouch = ShouldReceiveTouch;
            }
#endif

            for (int i = 0; i < ElementGestureRecognizers.Count; i++)
            {
                IGestureRecognizer recognizer = ElementGestureRecognizers[i];
                if (_gestureRecognizers.ContainsKey(recognizer))
                {
                    continue;
                }

                var nativeRecognizer = GetNativeRecognizer(recognizer);
                if (nativeRecognizer != null)
                {
#if __MOBILE__
                    nativeRecognizer.ShouldReceiveTouch = _shouldReceiveTouch;
#endif
                    _handler.AddGestureRecognizer(nativeRecognizer);

                    _gestureRecognizers[recognizer] = nativeRecognizer;
                }
            }

            var toRemove = _gestureRecognizers.Keys.Where(key => !ElementGestureRecognizers.Contains(key)).ToArray();

            for (int i = 0; i < toRemove.Length; i++)
            {
                IGestureRecognizer gestureRecognizer = toRemove[i];
                var uiRecognizer = _gestureRecognizers[gestureRecognizer];
                _gestureRecognizers.Remove(gestureRecognizer);

                _handler.RemoveGestureRecognizer(uiRecognizer);
                uiRecognizer.Dispose();
            }
        }
        void LoadRecognizers()
        {
            if (ElementGestureRecognizers == null)
            {
                return;
            }

            foreach (var recognizer in ElementGestureRecognizers)
            {
                if (_gestureRecognizers.ContainsKey(recognizer))
                {
                    continue;
                }

                var nativeRecognizer = GetNativeRecognizer(recognizer);
                if (nativeRecognizer != null)
                {
#if __MOBILE__
                    nativeRecognizer.ShouldReceiveTouch = _shouldReceive;
#endif
                    _handler.AddGestureRecognizer(nativeRecognizer);

                    _gestureRecognizers[recognizer] = nativeRecognizer;
                }
            }

            var toRemove = _gestureRecognizers.Keys.Where(key => !ElementGestureRecognizers.Contains(key)).ToArray();
            foreach (var gestureRecognizer in toRemove)
            {
                var uiRecognizer = _gestureRecognizers[gestureRecognizer];
                _gestureRecognizers.Remove(gestureRecognizer);

                _handler.RemoveGestureRecognizer(uiRecognizer);
                uiRecognizer.Dispose();
            }
        }
Beispiel #3
0
        void LoadRecognizers()
        {
            if (ElementGestureRecognizers == null)
            {
                return;
            }

#if __MOBILE__
            if (_shouldReceiveTouch == null)
            {
                // Cache this so we don't create a new UITouchEventArgs instance for every recognizer
                _shouldReceiveTouch = ShouldReceiveTouch;
            }
#endif

#if __MOBILE__
            UIDragInteraction uIDragInteraction = null;
            UIDropInteraction uIDropInteraction = null;

            if (_dragAndDropDelegate != null)
            {
                foreach (var interaction in _renderer.NativeView.Interactions)
                {
                    if (interaction is UIDragInteraction uIDrag && uIDrag.Delegate == _dragAndDropDelegate)
                    {
                        uIDragInteraction = uIDrag;
                    }

                    if (interaction is UIDropInteraction uiDrop && uiDrop.Delegate == _dragAndDropDelegate)
                    {
                        uIDropInteraction = uiDrop;
                    }
                }
            }

            bool dragFound = false;
            bool dropFound = false;
#endif
            for (int i = 0; i < ElementGestureRecognizers.Count; i++)
            {
                IGestureRecognizer recognizer = ElementGestureRecognizers[i];
                if (_gestureRecognizers.ContainsKey(recognizer))
                {
                    continue;
                }

                var nativeRecognizer = GetNativeRecognizer(recognizer);
                if (nativeRecognizer != null && _handler != null)
                {
#if __MOBILE__
                    nativeRecognizer.ShouldReceiveTouch = _shouldReceiveTouch;
#endif
                    _handler.AddGestureRecognizer(nativeRecognizer);

                    _gestureRecognizers[recognizer] = nativeRecognizer;
                }

#if __MOBILE__
                if (Forms.IsiOS11OrNewer && recognizer is DragGestureRecognizer)
                {
                    dragFound            = true;
                    _dragAndDropDelegate = _dragAndDropDelegate ?? new DragAndDropDelegate();
                    if (uIDragInteraction == null)
                    {
                        var interaction = new UIDragInteraction(_dragAndDropDelegate);
                        interaction.Enabled = true;
                        _renderer.NativeView.AddInteraction(interaction);
                    }
                }

                if (Forms.IsiOS11OrNewer && recognizer is DropGestureRecognizer)
                {
                    dropFound            = true;
                    _dragAndDropDelegate = _dragAndDropDelegate ?? new DragAndDropDelegate();
                    if (uIDropInteraction == null)
                    {
                        var interaction = new UIDropInteraction(_dragAndDropDelegate);
                        _renderer.NativeView.AddInteraction(interaction);
                    }
                }
#endif
            }

#if __MOBILE__
            if (!dragFound && uIDragInteraction != null)
            {
                _renderer.NativeView.RemoveInteraction(uIDragInteraction);
            }

            if (!dropFound && uIDropInteraction != null)
            {
                _renderer.NativeView.RemoveInteraction(uIDropInteraction);
            }
#endif

            var toRemove = _gestureRecognizers.Keys.Where(key => !ElementGestureRecognizers.Contains(key)).ToArray();

            for (int i = 0; i < toRemove.Length; i++)
            {
                IGestureRecognizer gestureRecognizer = toRemove[i];
                var uiRecognizer = _gestureRecognizers[gestureRecognizer];
                _gestureRecognizers.Remove(gestureRecognizer);

                _handler.RemoveGestureRecognizer(uiRecognizer);
                uiRecognizer.Dispose();
            }
        }