UIPageViewController常用多页管理中,可能会碰到滑动手势与子页面中的UIScrollView滚动视图出现冲突。
下图是我们需要的效果
自定义一个scrollview 看代码就ok了
class PanScroll: UIScrollView,UIGestureRecognizerDelegate {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if let swip = otherGestureRecognizer as? UIPanGestureRecognizer{
if self.contentOffset.x <= 1 && self.commitTranslation(translation: swip.translation(in: self)) == .right {
return true
}
if self.contentOffset.x >= UIScreen.main.bounds.size.width && self.commitTranslation(translation: swip.translation(in: self)) == .left {
return true
}
}
return false
}
///MARK: pan手势方向
enum panDic {
case upward,down,left,right,none
}
func commitTranslation(translation:CGPoint ) ->panDic{
let absX = fabs(translation.x)
let absY = fabs(translation.y)
// 设置滑动有效距离
if max(absX, absY) < 2{
return .none
}
if absX > absY {
if (translation.x<0) {return .left}else{return .right}
} else if absY > absX {
if (translation.y<0) {return .upward}else{return .down}
}
return .none
}
}