Exemplo n.º 1
0
        void GestureRecognizersOnCollectionChanged(object?sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
        {
            if (_platformView != null)
            {
                _platformView.AccessibilityTraits &= ~_addedFlags;

                if (_defaultAccessibilityRespondsToUserInteraction != null)
                {
                    _platformView.AccessibilityRespondsToUserInteraction = _defaultAccessibilityRespondsToUserInteraction.Value;
                }
            }

            _addedFlags = UIAccessibilityTrait.None;
            _defaultAccessibilityRespondsToUserInteraction = null;
            LoadRecognizers();
        }
Exemplo n.º 2
0
        void LoadRecognizers()
        {
            if (ElementGestureRecognizers == null)
            {
                return;
            }

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

            CalculateUserInteractionEnabled();
            UIDragInteraction?uIDragInteraction = null;
            UIDropInteraction?uIDropInteraction = null;

            if (_dragAndDropDelegate != null && _platformView != null)
            {
                if (PlatformVersion.IsAtLeast(11))
                {
                    foreach (var interaction in _platformView.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;

            if (_platformView != null &&
                _handler.VirtualView is View v &&
                v.TapGestureRecognizerNeedsDelegate() &&
                (_platformView.AccessibilityTraits & UIAccessibilityTrait.Button) != UIAccessibilityTrait.Button)
            {
                _platformView.AccessibilityTraits |= UIAccessibilityTrait.Button;
                _addedFlags |= UIAccessibilityTrait.Button;
                if (PlatformVersion.IsAtLeast(13))
                {
                    _defaultAccessibilityRespondsToUserInteraction       = _platformView.AccessibilityRespondsToUserInteraction;
                    _platformView.AccessibilityRespondsToUserInteraction = true;
                }
            }

            for (int i = 0; i < ElementGestureRecognizers.Count; i++)
            {
                IGestureRecognizer recognizer = ElementGestureRecognizers[i];

                if (_gestureRecognizers.ContainsKey(recognizer))
                {
                    continue;
                }

                var nativeRecognizer = GetPlatformRecognizer(recognizer);

                if (nativeRecognizer != null && _platformView != null)
                {
                    nativeRecognizer.ShouldReceiveTouch = _shouldReceiveTouch;
                    _platformView.AddGestureRecognizer(nativeRecognizer);

                    _gestureRecognizers[recognizer] = nativeRecognizer;
                }

                if (PlatformVersion.IsAtLeast(11) && recognizer is DragGestureRecognizer)
                {
                    dragFound            = true;
                    _dragAndDropDelegate = _dragAndDropDelegate ?? new DragAndDropDelegate(_handler);
                    if (uIDragInteraction == null && _handler.PlatformView != null)
                    {
                        var interaction = new UIDragInteraction(_dragAndDropDelegate);
                        interaction.Enabled = true;
                        _handler.PlatformView.AddInteraction(interaction);
                    }
                }

                if (PlatformVersion.IsAtLeast(11) && recognizer is DropGestureRecognizer)
                {
                    dropFound            = true;
                    _dragAndDropDelegate = _dragAndDropDelegate ?? new DragAndDropDelegate(_handler);
                    if (uIDropInteraction == null && _handler.PlatformView != null)
                    {
                        var interaction = new UIDropInteraction(_dragAndDropDelegate);
                        _handler.PlatformView.AddInteraction(interaction);
                    }
                }
            }
            if (PlatformVersion.IsAtLeast(11))
            {
                if (!dragFound && uIDragInteraction != null && _handler.PlatformView != null)
                {
                    _handler.PlatformView.RemoveInteraction(uIDragInteraction);
                }

                if (!dropFound && uIDropInteraction != null && _handler.PlatformView != null)
                {
                    _handler.PlatformView.RemoveInteraction(uIDropInteraction);
                }
            }

            var toRemove = new List <IGestureRecognizer>();

            foreach (var key in _gestureRecognizers.Keys)
            {
                if (!ElementGestureRecognizers.Contains(key))
                {
                    toRemove.Add(key);
                }
            }

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

                if (_platformView != null)
                {
                    _platformView.RemoveGestureRecognizer(uiRecognizer);
                }

                uiRecognizer.Dispose();
            }
        }