ios - UIPageViewController:表格垂直滚动不起作用

标签 ios swift swift3 uipageviewcontroller

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

最佳答案

如果您添加了 PanGestureRecognizer,它会尝试覆盖 tableView 的手势识别器,您必须使用 UIGestureRecognizerDelegate 进行手动检查。

let pangGesture = UIPanGestureRecognizer(target: self, action: #selector(createTxtPageViewController(recognizer:)))
pangGesture.delegate = self
self.view.addGestureRecognizer(pangGesture)

然后你必须实现gestureRecongizerShouldBegin委托(delegate)函数

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
    }

}

关于ios - UIPageViewController:表格垂直滚动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43856322/

相关文章:

ios - 在没有Crashlytics Mac App的情况下进行编译?

ios - TabView 背景透明度在 iOS 15 上的 VStack 内的 ScrollView 中无法按预期工作

ios - 如何在Xcode中通过自定义框架访问cocoapods

swift - 如何在 Swift 3 的 UITextView 中输出结果?

iOS/swift : mapView won't update coordinates

ios - 如何在自动布局 ios 中将 View 的底部边缘和顶部边缘约束到另一个 View ?

ios - 将我的 YouTube channel 获取(获取)到我的 iOS 应用程序中

swift - 使用 fromCString 从 DS 读取时解包 Optional 值时意外发现 nil

swift - 更改所选 UICollectionView 单元格的大小

ios - 如何从 json 按日期排序帖子