How to detect tap gesture on chart marker?

纵然是瞬间 提交于 2019-12-11 06:36:51

问题


I am using Charts to create custom graphs in an application. The one thing I am having trouble with is detecting touch events on the chart marker that is presented for the current value. I want to perform and action based on the marker that was tapped. The action should take in the data entry represented by the tapped marker.

I have read through Selectable marker view & images at Y axis, but still haven't been able to produce a viable solution.

It would be great if someone could provide a code sample, or more detailed explanation than found in the link above, to point me in the right direction.

Thanks


回答1:


As per your requirement Charts library already have that method in its delegate.

Please check below code :

  1. Assign delegate to your ChartView like below
lineChartView.delegate = self
  1. Implement Delegate method in your class
public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {

}

Here you can write your code while user click on chart.

Hope this info will helps!




回答2:


I ended up solving this by subclassing the ChartView of my choice and overriding the existing tap gesture recognizer with my own.

Example

final class CustomChartView: BarChartView {
    ...
    private func initialize() {
        ...
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapRecognized))
        self.addGestureRecognizer(tap)
    }

    @objc func tapRecognized(_ recognizer: UITapGestureRecognizer) {
        guard data !== nil else { return }

        switch recognizer.state {
            case .ended:
            // Detect whether or not the touch was inside a marker that was being presented
            // Otherwise, add/remove highlight as necessary

            if let marker = self.marker as? BalloonMarker {
                let location = recognizer.location(in: self)

                if !highlighted.isEmpty && marker.rect.contains(location) {
                    // In my case, I created custom property 'vertex' on BalloonMarker for easy reference to get `xValue`
                    let xValue = self.getTransformer(forAxis: .left).valueForTouchPoint(marker.vertex).x

                    // Do what you need to here with tap (call function, create custom delegate and trigger it, etc)
                    // In my case, my chart has a marker tap delegate
                    // ex, something like: `markerTapDelegate?.tappedMarker()`

                    return
                }
            }

            // Default tap handling
            guard isHighLightPerTapEnabled else { return }

            let h = getHighlightByTouchPoint(recognizer.location(in: self))

            if h === nil || h == self.lastHighlighted {
                lastHighlighted = nil
                highlightValue(nil, callDelegate: true)

            } else {
                lastHighlighted = h
                highlightValue(h, callDelegate: true)
            }

        default:
            break
    }
}


来源:https://stackoverflow.com/questions/51921792/how-to-detect-tap-gesture-on-chart-marker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!