如何检测图表标记上的点击手势? [英] How to detect tap gesture on chart marker?

查看:52
本文介绍了如何检测图表标记上的点击手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用图表在应用程序中创建自定义图形.我遇到的一件事是检测图表标记上针对当前值显示的触摸事件.我想根据被点击的标记执行和操作.该动作应输入由点击标记表示的数据条目.

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.

我已阅读可选标记视图&图像在Y轴上,但仍然无法产生可行的解决方案.

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.

谢谢

推荐答案

我最终解决了这一问题,方法是对自己选择的ChartView进行了子类化,并用我自己的覆盖了现有的轻敲手势识别器.

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

示例

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
    }
}

这篇关于如何检测图表标记上的点击手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆