locked
Swipe gesture recognizer working on iPad but not on iPhone RRS feed

  • Question

  • User155051 posted

    I have a dependency service for my iOS project to handle the different gesture recognizers in a Scroll view. I'm using this Scroll view to be able to double tap to zoom, pan on an image and swipe to another view when the zoom scale is set to 1. This is currently working as expected on an iPad, but when I run this on an iPhone all of the gesture recognizers work with the exception of the UISwipeGestureRecognizers. Here's what my dependency service file looks like...

    [assembly: ExportRenderer(typeof(PageScrollView), typeof(PageScrollViewIos))]
    namespace Sample.iOS.Views
    {
        class PageScrollViewIos : ScrollViewRenderer
        {
            UISwipeGestureRecognizer _swipeRightGestureRecognizer;
            UISwipeGestureRecognizer _swipeLeftGestureRecognizer;
            UITapGestureRecognizer _doubleTapGestureRecognizer;
    
            public PageScrollViewIos()
            {
                this.MinimumZoomScale = 1;
                this.BouncesZoom = false;
                _swipeLeftGestureRecognizer = new UISwipeGestureRecognizer(() => UpdateRight())
                {
                    Direction = UISwipeGestureRecognizerDirection.Right
                };
                _swipeRightGestureRecognizer = new UISwipeGestureRecognizer(() => UpdateLeft())
                {
                    Direction = UISwipeGestureRecognizerDirection.Left
                };
                _doubleTapGestureRecognizer = new UITapGestureRecognizer(() => DoubleTapToZoom())
                {
                    NumberOfTapsRequired = 2
                };
            }
    
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                if (e.OldElement == null)
                {
                    this.AddGestureRecognizer(_swipeRightGestureRecognizer);
                    this.AddGestureRecognizer(_swipeLeftGestureRecognizer);
                    this.AddGestureRecognizer(_doubleTapGestureRecognizer);
                    this.MaximumZoomScale = 4f;
                    this.MinimumZoomScale = 1f;
                    this.ViewForZoomingInScrollView += (UIScrollView sv) =>
                    {
                        return this.Subviews[0];
                    };
                }
    
                if (e.NewElement == null)
                {
                    if (_swipeLeftGestureRecognizer != null)
                    {
                        this.RemoveGestureRecognizer(_swipeLeftGestureRecognizer);
                    }
                    if (_swipeRightGestureRecognizer != null)
                    {
                        this.RemoveGestureRecognizer(_swipeRightGestureRecognizer);
                    }
                    if (_doubleTapGestureRecognizer != null)
                    {
                        this.RemoveGestureRecognizer(_doubleTapGestureRecognizer);
                    }
                }
            }
    
            // This does not get called when running on an iPhone
            private void UpdateLeft()
            {
                if (this.ZoomScale == 1)
                {
                    // Do stuff here when zoom scale is 1
                }
    
            }
    
            // This does not get called when running on an iPhone
            private void UpdateRight()
            {
                if (this.ZoomScale == 1)
                {
                    // Do stuff here when zoom scale is 1
                }
            }
    
            private void DoubleTapToZoom()
            {
                if (this.ZoomScale == 1)
                {
                    this.SetZoomScale(2f, true);
                }
                else
                {
                    this.SetZoomScale(1f, true);
                }
            }   
        }
    }
    

    Why is it that the swipe gesture recognizer doesn't work on an iPhone?

    Monday, April 25, 2016 2:41 PM

Answers

  • User155051 posted

    It turns out the issue didn't have anything to do with my dependency service but with the MasterDetailPage that I'm using. I have a MasterDetailPage as my main page, and to open and close the Master page, it comes with a gesture recognizer which I suspect was overriding the dependency service swipe gesture recognizer. To fix the issue, all I had to do was set the MasterDetailPage property 'IsGestureEnabled' to false

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 26, 2016 6:04 PM

All replies

  • User56015 posted

    Hi @rarenivar ,

    Currently in between breaks at Xamarin Evolve, so I've to answer a bit quickly :-). By looking at the code I honestly can't see straight away why it isn't working, but maybe this repo helps you out: https://github.com/Manne990/PhotoViewerTest

    I think it's roughly what you need already. Credits to Jonas Frid ofcourse :)

    Monday, April 25, 2016 4:14 PM
  • User155051 posted

    @MittchelvanVliet - Thank you for your response, unfortunately I didn't find anything that could help me in that repo.

    Monday, April 25, 2016 8:20 PM
  • User56015 posted

    @rarenivar The repo has all the functionality you're mentioning, so you could decide to use this.

    Monday, April 25, 2016 9:14 PM
  • User155051 posted

    It turns out the issue didn't have anything to do with my dependency service but with the MasterDetailPage that I'm using. I have a MasterDetailPage as my main page, and to open and close the Master page, it comes with a gesture recognizer which I suspect was overriding the dependency service swipe gesture recognizer. To fix the issue, all I had to do was set the MasterDetailPage property 'IsGestureEnabled' to false

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 26, 2016 6:04 PM