Search code examples
iosswiftios-charts

UIRotationGestureRecgnizer delegate methods not triggered


im using the DGCharts library to display a pie chart. This chart is within a vertical scrollview, and the chart has the ability to rotate around its axis. This works with a single swipe, however the scrollviews swipe/pan gesture gets in the way and takes over. I would like to prevent the scollviews gesture recognizer when the chart is being rotated.

The pie chart has a UIRotationGestureRecognizer and a UITapGestureRecognizer. Using UIGestureRecognizer delegate methods i tried

    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        // Allow the rotation gesture to work alongside the scroll view's pan gesture
        if gestureRecognizer is UIRotationGestureRecognizer || otherGestureRecognizer is UIRotationGestureRecognizer {
            return true
        }
        return false
    }
    
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if gestureRecognizer == scrollView.panGestureRecognizer {
            // If the pan gesture recognizer is triggered and the touch is inside `yourView`, prevent the scroll view from scrolling
            let locationInView = touch.location(in: pieChartView)
            if pieChartView.bounds.contains(locationInView) {
                return false // Disable the scroll view's pan gesture when touching `yourView`
            }
        }

        // Otherwise, allow the gesture to be received as usual
        return true
    }

and i also tried scrollView.panGestureRecognizer.require(toFail: rotationRecognizer)

both of which are not successful.

i did set the delegate:

        if let pieViewRecognizers = pieChartView.gestureRecognizers {
            for gestureRecognizer in pieViewRecognizers {
                if gestureRecognizer is UIRotationGestureRecognizer {
                    gestureRecognizer.delegate = self
                }
            }
        }

Primarily the issue seems to be that UIRotationGestureRecognizer expects a two finger touch to trigger the delegate methods needed to disable the scrollview when in use, but i can swipe and rotate the chart with a single finger and id like to prevent the scrollview from taking over if a single finger swipes the chart.

What else can i try to stop the scrollview from moving around when im trying to rotate the chart?


Solution

  • The library has a .rotationWithTwoFingers property for this specific reason. Comments on this property state flag that indicates if rotation is done with two fingers or one. when the chart is inside a scrollview, you need a two-finger rotation because a one-finger rotation eats up all touch events.