我也在为此苦苦挣扎。由于我需要图表可以滚动,@Entrabiter 的解决方案对我不起作用。唯一对我有用的解决方案是将图表视图的委托分配UIPanGestureRecognizer
给我的 ViewController 并实现UIGestureRecognizerDelegate
。
这个解决方案在 Swift 中,但在 Objective-C 中也应该可以正常工作。
class MyViewController: UIViewController, UIGestureRecognizerDelegate {
// MARK: Outlets
@IBOutlet weak var contentView: UIScrollView!
@IBOutlet weak var myChart: LineChartView!
override func viewDidLoad() {
super.viewDidLoad()
if let gestureRecognizers = myChart.gestureRecognizers {
for gestureRecongnizer in gestureRecognizers {
if gestureRecongnizer is UIPanGestureRecognizer {
gestureRecongnizer.delegate = self
}
}
}
}
}
重要的部分是告诉gestureRecognizer 识别scrollview 的panGestureRecognizer 以及chartview 的panGestureRecognizer。
// MARK: UIGestureRecognizerDelegate
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if otherGestureRecognizer == contentView.panGestureRecognizer {
return true
}
return false
}