无需触摸即可手动启动 UIPanGestureRecognizer
Manually starting a UIPanGestureRecognizer without touch
我有一个 table 视图没有覆盖整个屏幕(有点像屏幕底部的抽屉)。当用户向下滚动到内容的末尾时,我想停止滚动,然后添加一个平移手势识别器。我是这样做的:
// 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)
}
}
}
手势已添加,但在用户再次触摸屏幕之前不会注册。他们的手指已经在 table 视图上。是否可以在他们无需再次触摸屏幕的情况下启动手势?
我认为您对 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
}
我有一个 table 视图没有覆盖整个屏幕(有点像屏幕底部的抽屉)。当用户向下滚动到内容的末尾时,我想停止滚动,然后添加一个平移手势识别器。我是这样做的:
// 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)
}
}
}
手势已添加,但在用户再次触摸屏幕之前不会注册。他们的手指已经在 table 视图上。是否可以在他们无需再次触摸屏幕的情况下启动手势?
我认为您对 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
}