首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将UIPanGesture添加到UITableView会阻止Xcode11.3中的UITableView滚动

将UIPanGesture添加到UITableView会阻止Xcode11.3中的UITableView滚动
EN

Stack Overflow用户
提问于 2020-01-15 23:54:44
回答 1查看 143关注 0票数 1

我和UITableView有UIViewController。当用户拉下表格时,我想取消UIViewController。通过将UIPanGestureRecognizer添加到UIViewController,只有在表内容较少且表不可滚动的情况下,才能使用控制器的视图。所以我在tableView中添加了UIPanGestureRecognizer:

代码语言:javascript
复制
self.detailTableView.bounces = true
let gesture = UIPanGestureRecognizer(target: self, action: #selector(onPan(_:)))
gesture.delegate = self
self.detailTableView.gestureRecognizers = [gesture]

onPan方法:

代码语言:javascript
复制
@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退回属性设置为true时,表在所有方向上都会退回。通过仅启用垂直方向的反弹不起作用。

代码语言:javascript
复制
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
    }

这段代码在Xcode10.2.1中工作得很好。现在我已经更新到Xcode11.3,dismiss正在工作,但它阻止了tableView的滚动。

有人能提供解决方案吗?

提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2020-02-07 20:00:33

您可以尝试使用以下方法来处理手势识别器。通过这样做,一旦找到附加的视图手势和手势类型,您就可以实现向下拖动以取消功能。

代码语言:javascript
复制
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
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59755068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档