ShinobiCharts数据标签/注释/刻度线手势识别器



使用忍者图表

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

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

我尝试了这个勾号/数据点标签,但没有运气:

 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,不知道如何在那里添加一个

我想你快有了标签。我发现我只需要设置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 添加到图表中,然后循环访问图表的注释以检查触摸点是否在注释内。 例如:

在视图中DidLoad:

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

然后是注解功能:

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

相关内容

  • 没有找到相关文章

最新更新