0

使用 shinobi 图表

寻找如何将手势识别器(在修饰时)添加到刻度标记和图表注释的示例

我看到了与系列数据系列交互的文档,但我需要将 GestureRecognizers 添加到刻度线和注释事件

我为 tickMark/datapoint 标签尝试了这个,但没有运气:

 func sChart(chart: ShinobiChart!, alterTickMark tickMark: SChartTickMark!, beforeAddingToAxis axis: SChartAxis!) {

    if let label = tickMark.tickLabel {
        //added a gesture recognizer here but it didn't work
    }

对于 SchartAnnotations 不知道如何在那里添加一个

4

1 回答 1

0

我认为你几乎与标签在那里。我发现我只需要设置userInteractionEnabled = true例如

func sChart(chart: ShinobiChart!, alterTickMark tickMark: SChartTickMark!, beforeAddingToAxis axis: SChartAxis!) {
    if let label = tickMark.tickLabel {

        let tapRecognizer = UITapGestureRecognizer(target: self, action: "labelTapped")
        tapRecognizer.numberOfTapsRequired = 1
        label.addGestureRecognizer(tapRecognizer)

        label.userInteractionEnabled = true
    }
}

注释有点棘手,因为它们位于 SChartCanvasOverlay 下方的视图中(负责监听手势)。这导致手势在到达注释之前被“吞下”。

但是,这是可能的,但您需要将 UITapGestureRecognizer 添加到图表中,然后遍历图表的注释以检查触摸点是否在注释内。例如:

在 viewDidLoad 中:

let chartTapRecognizer = UITapGestureRecognizer(target: self, action: "annotationTapped:")
chartTapRecognizer.numberOfTapsRequired = 1
chart.addGestureRecognizer(chartTapRecognizer)

然后是 annotationTapped 函数:

func annotationTapped(recognizer: UITapGestureRecognizer) {

    var touchPoint: CGPoint?

    // Grab the first annotation so we can grab its superview for later use
    if let firstAnnotation = chart.getAnnotations().first as? UIView {
        // Convert touch point to position on annotation's superview

        let glView = firstAnnotation.superview!

        touchPoint = recognizer.locationInView(glView)
    }

    if let touchPoint = touchPoint {

        // Loop through the annotations
        for item in chart.getAnnotations() {
            let annotation: SChartAnnotation = item as SChartAnnotation

            if (CGRectContainsPoint(annotation.frame, touchPoint as CGPoint)) {
                chart.removeAnnotation(annotation)
            }
        }
    }
}
于 2015-04-16T08:15:25.747 回答