使用shinobi图表
寻找示例如何添加手势识别器(在触摸屏上)以勾选标记和schart annoations
我看到了与系列数据系列交互的文档,但我需要将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,不知道如何添加一个
答案 0 :(得分: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,然后循环显示图表的注释,以检查触摸点是否在注释中。 E.g:
在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)
}
}
}
}