美文网首页1
解决UIScrollView不能侧滑返回

解决UIScrollView不能侧滑返回

作者: asdfeng | 来源:发表于2017-07-03 17:11 被阅读34次

当我们的APP里使用一个在水平方向上滑动的UIScrollView时,会导致系统的侧滑返回功能响应失效,这是因为UIScrollView和侧滑返回共用一个UIPanGestureRecognizer手势响应事件,UIScrollView拦截了这个事件并做出了响应。我们只需要创建UIScrollView的一个分类并重写gestureRecognizerShouldBegin方法即可。

extension UIScrollView {
    // 解决有UIScrollView时不能在屏幕左边侧滑返回
    open override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        // 过滤UITextView(因为UITextView继承自UIScrollView),否则会引起崩溃
        if (gestureRecognizer.view?.isMember(of: UITextView.self))! {
            return true
        }
        let velocity = (gestureRecognizer as! UIPanGestureRecognizer).velocity(in: self)
        let location = gestureRecognizer.location(in: self)
        if (velocity.x > 0.0 && Int(location.x) % Int(UIScreen.main.bounds.size.width) < 60) {
            return false
        }
        return true
    }
}

相关文章

网友评论

    本文标题:解决UIScrollView不能侧滑返回

    本文链接:https://www.haomeiwen.com/subject/dzzacxtx.html