我有一个不能覆盖整个屏幕的表格视图(有点像屏幕底部的抽屉)。当用户向下滚动到内容的末尾时,我要停止滚动,然后添加平移手势识别器。我这样做是这样的:
// MARK: UIScrollViewDelegate Methods
extension TutorProfileVC: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Limit top vert bounce
guard mode == .drawer else { return }
if scrollView.contentOffset.y < -80.0 {
scrollView.contentOffset = CGPoint(x: 0, y: -80.0)
tableView.addGestureRecognizer(tablePanGR)
}
}
}
已经添加了手势,但是直到用户再次触摸屏幕后才会注册。他们的手指已经在桌子上了。是否可以在无需再次触摸屏幕的情况下开始手势?
答案 0 :(得分:1)
我认为您在this question上也遇到同样的问题。如果您想查看代码示例,请看一下。
要解决问题,您应该从头开始添加手势,但仅在用户滚动到底部时处理手势动作。因此,您无需再次触摸屏幕,因为在开始滚动时便会启动手势。手势处理方法如下所示
@objc func handlePanGestureRecognizer(_ gestureRecognizer: UIPanGestureRecognizer) {
switch gestureRecognizer.state {
case .began:
// Do nothing
break
case .changed:
let translation = gestureRecognizer.translation(in: gestureRecognizer.view!.superview!)
let velocity = gestureRecognizer.velocity(in: gestureRecognizer.view!.superview)
let state = gestureRecognizer.state
// Don't do anything until |scrollView| reached bottom
if scrollView.contentOffset.y >= -80.0 {
return;
}
// Do whatever you want with |scrollView|
}
break;
case .cancelled:
case .ended:
case .failed:
default:
break;
}
}
还实现gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
以使手势和滚动视图协同工作
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}