Search code examples
androidioschartsmpandroidchartios-charts

Is there a way to handle gesture end (touch up) in iOS version of MPAndroidChart framework?


In Android version of MPAndroidChart framework there's a method called onChartGestureEnd which triggers on touch up (e.g. when you finished dragging your line chart).

Is there an analogue of this method in iOS version of this framework (Charts). If no then how can I handle end of dragging (maybe with UIPanGestureRecognizer) without damaging the current dragging behaviour of my chart?


Solution

  • One of possible solutions was to add a UIPanGestureRecognizer to the chart and add whatever behaviour you need if the gesture is finished:

    @IBAction func didDragChart(_ sender: UIPanGestureRecognizer) {
        switch sender.state {
        case .ended, .cancelled:
            // Whatever you want to do when finished dragging.
        default:
            break
        }
    }
    

    However it's not enough since charts in this framework already have a default behaviour for dragging. To allow more than one gesture of the same kind (in our case it's dragging) you can create a new subclass of your chart and override this method:

     override func gestureRecognizer(_ gestureRecognizer: NSUIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: NSUIGestureRecognizer) -> Bool {
            return true
        }
    

    Now the both scrolling the chart with dragging (the default behaviour) and your gesture recognizer will work together.