我有一个带有四个标签的UIPageViewController。三个表和一个webview。问题是垂直滚动对它们不起作用。如何使用swift解决这个问题?
这是我的代码:
func createTxtPageViewController() {
let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("pagetxtController") as! UIPageViewController
pageController.dataSource = self
pageController.delegate = self
if texts.count > 0 {
let firstController = getItemTxtController(0)!
let startingViewControllers = [firstController]
pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
pageTxtViewController = pageController
pageTxtViewController?.view.frame = CGRectMake(10, 173, self.rightView.frame.size.width - 20, self.rightView.frame.size.height - 183)
addChildViewController(pageTxtViewController!)
self.rightView.addSubview(pageTxtViewController!.view)
pageTxtViewController!.didMoveToParentViewController(self)
}
答案 0 :(得分:0)
如果您添加了PanGestureRecognizer,它会尝试覆盖tableView的手势识别器,您必须使用UIGestureRecognizerDelegate进行手动检查。
let pangGesture = UIPanGestureRecognizer(target: self, action: #selector(createTxtPageViewController(recognizer:)))
pangGesture.delegate = self
self.view.addGestureRecognizer(pangGesture)
然后你必须实现gestureRecongizerShouldBegin委托函数
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
// we check for panGesture if it's panGesture we continue if not return
guard let recognizer = gestureRecognizer as? UIPanGestureRecognizer else {
return true
}
// The velocity of the pan gesture in the coordinate system of the specified view.
let velocity = recognizer.velocity(in: self.view)
// If velocity.y is greater than velocity.x user is trying to scroll tableView else user wants to swipe to next screen
return abs(velocity.y) > abs(velocity.x) ? false : true
}
在类的顶部为touchLocation声明一个CGPoint属性
var panStartPoint: CGPoint!
现在在 createTxtPageViewController 功能中检查状态并检测用户向左或向右滑动方向
func createTxtPageViewController(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
touchLocation = recognizer.location(in: self.view)
case .changed:
let currentPoint = recognizer.location(in: self.view)
let swipingLeft = currentPoint.x < touchLocation.x ? true : false
if !swipingLeft {
// swiped right
//TODO: Add your logic for navigation to nextViewController
} else {
// swiped left
//TODO: Add your logic for navigation to nextViewController
}
// you can check for other states as well and clean up after your self e.g state.cancelled I just break
default:
break
}
}