ios - 手势不适用于包含 UITableViewControllers 的 UIPageViewController

标签 ios swift uitableview uigesturerecognizer uipageviewcontroller

我正在尝试为我的应用实现类似通知中心的功能:

有一个 UIPageViewController,因为我想让左/右滑动手势起作用,并且在里面我需要显示 4 个 UITableViewControllers。顶部有一个 UISegmentedCotrol 来显示“选择了哪个选项卡”

一切正常,除了用于更改页面的滑动手势,就好像 TableView 正在捕获手势而不是让 PageView 完成它的工作。

我不需要我的 TableView 是可编辑的,因为它们只是来自网站的文章。只有 didSelectRowAt(indexPath) 足以执行我需要的任何操作(转至“articleViewController”)。

如果有人知道如何让 UIPageViewController 的手势与 TableViewController 一起工作,请告诉我。谢谢

最佳答案

var pageviewController = UIPageViewController()

pageviewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)

    pageviewController.view.frame = CGRect(x: 0, y: 0, width: 320 , height: 568)

    pageviewController.setViewControllers([arrayControllers[0]], direction: .forward, animated: false, completion: nil)

    addChildViewController(pageviewController)
    viewToAdd.addSubview(pageviewController.view)
    pageviewController.didMove(toParentViewController: self)

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right

    self.view.addGestureRecognizer(swipeRight)
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(swipeLeft)

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            //right view controller

            print("Swipe Right")

        case UISwipeGestureRecognizerDirection.left:
            //left view controller

            print("Swipe left")

        default:
            break
        }
    }
}

关于ios - 手势不适用于包含 UITableViewControllers 的 UIPageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44105528/

相关文章:

ios - Protocol Buffers Swift 生成图像未找到

swift - 如何在 Swift 4.2 的另一个 View Controller 中删除 UICollectionView 中的选定/突出显示的单元格

ios - XCode 9 在打开所有 Storyboard文件时崩溃

ios - 为什么它一直说密码不匹配?

ios - 如何在UITableViewCell中居中放置图像?

ios - 具有自动布局左边距的 UITableViewCell 在 iPhone 和 iPad 上不同

ios - Swift - 按创建日期对 TableView 单元格进行排序

iphone - CustomCell 中的标签保持为零

ios - 核心数据中部分索引的整个字母表

ios - UITableView 重新排列单元格