Hekes Pekes
Hekes Pekes

Reputation: 1335

Charts iOS: How do I implement tap recognizing for LineChart label

I have got a wide LineChart with many entries. I want to let user tap (or better longtap/3D touch) on an entry to show modal card where user could edit data entry. I tried implementing chartValueSelected but the problem is that it runs even when user taps to scroll (i e taps without releasing finger) which is not how a button should behave. Is there any way to implement tap recognizing for LineChart label?

Upvotes: 3

Views: 1744

Answers (2)

AlexSmet
AlexSmet

Reputation: 2161

You can attach your own gesture recognizer to LineChartView and use method getHighlightByTouchPoint to get information about the selected point.

override func viewDidLoad() {
    // ...
    let longTapRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(onLongTap))
    lineChartView.addGestureRecognizer(longTapRecognizer)
    // ...
}

@objc func onLongTap(recognizer: UILongPressGestureRecognizer) {
    if recognizer.state == .ended {
        let highlight = lineChartView.getHighlightByTouchPoint(recognizer.location(in: lineChartView))
        print("\(highlight)")
    }
}

Upvotes: 2

developingbassist
developingbassist

Reputation: 116

It appears that overriding the tap gesture recognizer for the chart can work. This question has some answers based on someone who was looking for a similar solution.

Upvotes: 1

Related Questions