【问题标题】:UIPageViewController: table Vertical scroll does not workUIPageViewController:表格垂直滚动不起作用
【发布时间】:2017-05-08 19:58:23
【问题描述】:

我有一个带有四个选项卡的 UIPageViewController。三张表和一个网页视图。问题是垂直滚动对它们不起作用。如何使用 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)
    }

【问题讨论】:

  • 尝试更改添加容器视图的顺序self.rightView.addSubview(pageTxtViewController!.view) 然后addChildViewController(pageTxtViewController!)
  • @KhalidAfridi 这不起作用
  • 您的 pageViewController 中有滑动功能吗?
  • @KhalidAfridi 是的,我有表格和 webView。
  • 问题是您向 containerView 添加手势,这反过来又覆盖了 tableView 和 web 视图手势识别器

标签: ios swift swift3 uipageviewcontroller


【解决方案1】:

如果您添加了 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
    }

}

【讨论】:

  • 对不起,我不明白。我要用 Gesture 替换我的 UIPageViewController 吗?
  • 我认为你的 UIPageViewController 中已经有了滑动手势?
  • 是的,我有它,它可以工作。我不明白为什么要重做用于 UIPageViewController 创建的“createTxtPageViewController”方法。
  • 所以我知道我将手动重新创建运行 UIPageViewController 的手势,因为这将替换 tableView 的滚动。使表格正常滚动。是吗?
  • 这里是如何禁用 UIPageViews 滑动的答案 [How do i Disable the swipe] (stackoverflow.com/questions/22098493/…)
猜你喜欢
  • 2012-06-21
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 2016-12-20
  • 2018-09-04
  • 1970-01-01
相关资源
最近更新 更多