我有带有UITableView的UIViewController。我想在用户下拉表时关闭UIViewController。通过将UIPanGestureRecognizer添加到UIViewController的视图中,仅在表内容较少且表不可滚动时才有效。所以我将UIPanGestureRecognizer添加到tableView:
self.detailTableView.bounces = true
let gesture = UIPanGestureRecognizer(target: self, action: #selector(onPan(_:)))
gesture.delegate = self
self.detailTableView.gestureRecognizers = [gesture]
onPan方法:
@objc func onPan(_ panGesture: UIPanGestureRecognizer) {
guard self.detailTableView.contentOffset.y <= 0 else {
return
}
func slideViewVerticallyTo(_ yPoint: CGFloat) {
self.view.frame.origin = CGPoint(x: 0, y: yPoint)
}
switch panGesture.state {
case .began, .changed:
// If pan started or is ongoing then
// slide the view to follow the finger
let translation = panGesture.translation(in: view)
let yPoint = max(0, translation.y)
slideViewVerticallyTo(yPoint)
case .ended:
// If pan ended, decide it we should close or reset the view
// based on the final position and the speed of the gesture
let translation = panGesture.translation(in: view)
let velocity = panGesture.velocity(in: view)
let closing = (translation.y > self.view.frame.size.height / 2) ||
(velocity.y > minimumVelocityToHide)
if closing {
UIView.animate(withDuration: animationDuration, animations: {
// If closing, animate to the bottom of the view
slideViewVerticallyTo(self.view.frame.size.height)
}, completion: { (isCompleted) in
if isCompleted {
// Dismiss the view when it disappeared
// Dismiss UIViewController here....
}
})
} else {
// If not closing, reset the view to the top
UIView.animate(withDuration: animationDuration, animations: {
slideViewVerticallyTo(0)
})
}
default:
// If gesture state is undefined, reset the view to the top
UIView.animate(withDuration: animationDuration, animations: {
slideViewVerticallyTo(0)
})
}
}
还在委托后实现,因为当tableView bounce属性设置为true时,表会在所有方向上反弹。通过仅启用垂直方向弹跳是行不通的。
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if let panRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
// Ensure it's a Vertical drag
let velocity = panRecognizer.velocity(in: self.view)
if abs(velocity.y) < abs(velocity.x) {
return false
}
} else {
return false
}
return true
}
此代码在Xcode 10.2.1上运行良好。现在我已经更新到Xcode 11.3,dismiss可以正常工作,但是它阻止了tableView的滚动。
任何人都可以提供解决方案吗?
先谢谢您。
您可以尝试通过以下方法处理手势识别器。这样,一旦找到附加的查看手势和手势种类,就可以实现向下拖动以关闭功能。
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer.isKind(of: UIPanGestureRecognizer.self) && otherGestureRecognizer.isKind(of: UIPanGestureRecognizer.self) && gestureRecognizer.view == tableview && gestureRecognizer.view == tableview {
// your logic here
}
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer.isKind(of: UIPanGestureRecognizer.self) && otherGestureRecognizer.isKind(of: UIPanGestureRecognizer.self) && gestureRecognizer.view == tableview && gestureRecognizer.view == tableview {
// your logic here
}
return true
}
}