我使用 DGCharts 库来显示饼图。 该图表位于垂直滚动视图内,并且图表能够绕其轴旋转。 这只需一次滑动即可工作,但是滚动视图滑动/平移手势会妨碍并接管。 我想在图表旋转时阻止 scollviews 手势识别器。
饼图有一个 UIRotationGestureRecognizer 和一个 UITapGestureRecognizer。 使用我尝试过的 UIGestureRecognizer 委托方法
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Allow the rotation gesture to work alongside the scroll view's pan gesture
if gestureRecognizer is UIRotationGestureRecognizer || otherGestureRecognizer is UIRotationGestureRecognizer {
return true
}
return false
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if gestureRecognizer == scrollView.panGestureRecognizer {
// If the pan gesture recognizer is triggered and the touch is inside `yourView`, prevent the scroll view from scrolling
let locationInView = touch.location(in: pieChartView)
if pieChartView.bounds.contains(locationInView) {
return false // Disable the scroll view's pan gesture when touching `yourView`
}
}
// Otherwise, allow the gesture to be received as usual
return true
}
我也尝试过
scrollView.panGestureRecognizer.require(toFail: rotationRecognizer)
两者都没有成功。
我确实设置了委托:
if let pieViewRecognizers = pieChartView.gestureRecognizers {
for gestureRecognizer in pieViewRecognizers {
if gestureRecognizer is UIRotationGestureRecognizer {
gestureRecognizer.delegate = self
}
}
}
主要问题似乎是 UIRotationGestureRecognizer 需要两根手指触摸来触发使用时禁用滚动视图所需的委托方法,但我可以用单根手指和 id 滑动和旋转图表,以防止滚动视图接管如果单个手指滑动图表。
当我尝试旋转图表时,我还能尝试什么来阻止滚动视图移动?
出于这个特定原因,该库具有
.rotationWithTwoFingers
属性。对此财产状态的评论
flag that indicates if rotation is done with two fingers or one. when the chart is inside a scrollview, you need a two-finger rotation because a one-finger rotation eats up all touch events.