无需触摸手动启动 UIPanGestureRecognizer
Posted
技术标签:
【中文标题】无需触摸手动启动 UIPanGestureRecognizer【英文标题】:Manually starting a UIPanGestureRecognizer without touch 【发布时间】:2018-09-13 04:50:25 【问题描述】:我有一个不覆盖整个屏幕的表格视图(它有点像屏幕底部的抽屉)。当用户向下滚动到内容的末尾时,我想停止滚动,然后添加一个平移手势识别器。我是这样做的:
// 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)
手势已添加,但在用户再次触摸屏幕之前不会注册。他们的手指已经在桌面视图上。是否可以在无需再次触摸屏幕的情况下启动手势?
【问题讨论】:
【参考方案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
【讨论】:
如果我添加此手势,表格视图将不会滚动 您需要实现 shouldRecognizeSimultaneouslyWith 方法并返回 true 以使手势和滚动视图协同工作以上是关于无需触摸手动启动 UIPanGestureRecognizer的主要内容,如果未能解决你的问题,请参考以下文章