开发中,有部分UI,会将UIScrollView横向铺在底层,上面放tableView 或一些视图左右滚动切换,底层的scrollView会和Nav ViewController原有
开发中,有部分UI,会将UIScrollView横向铺在底层,上面放tableView 或一些视图左右滚动切换,底层的scrollView会和Nav ViewController原有的返回手势冲突
解决办法,重写UIScrollView 的gestureRecognizerShouldBegin,在ScrollView滚动到头的时候,屏蔽ScrollView的手势
class GesturesConflictScrollView: UIScrollView {
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
back(by: gestureRecognizer)
}
private final func back(by gestureRecognizer: UIGestureRecognizer) -> Bool {
guard gestureRecognizer == panGestureRecognizer else { return true }
// point.x < 0 代表左滑即手指从屏幕右向左移动 反之一样
let point: CGPoint = panGestureRecognizer.translation(in: self)
let state: UIGestureRecognizer.State = gestureRecognizer.state
let locDistance: CGFloat = UIScreen.main.bounds.size.width
if state == .began || state == .possible {
let locationPoint = gestureRecognizer.location(in: self)
if point.x > 0 && locationPoint.x < locDistance && contentOffset.x <= 0 {
return false
}
let pageCount = contentSize.width / UIScreen.main.bounds.size.width
let criticalPoint = pageCount < 2 ? locDistance : locDistance * (pageCount - 1)
if point.x < 0 && contentOffset.x == criticalPoint {
return false
}
}
return true
}
}
到此这篇关于iOS UIScrollView和控制器返回手势冲突解决方法的文章就介绍到这了,更多相关iOS UIScrollView和控制器手势冲突内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: iOS UIScrollView和控制器返回手势冲突解决方法
本文链接: https://lsjlt.com/news/30494.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0