首页 > 解决方案 > 我的 pageviewcontroller 手势是否被拦截?

问题描述

我已按照 pageviewcontroller 教程创建图像滑块。然后我将其调整为插入到 tableviewcell 中,因为我想要表格中的图像滑块。

我喜欢在全屏或嵌入到另一个视图中使用相同的代码的想法。

但是,当我这样做时,pageviewafter 和 pageviewbefore 事件在滑动时不会触发。我怀疑 tableviewcontroller 或单元格正在吞噬事件,因此 pageviewcontroller 没有收到它们。

我充其量是兼职 ios 开发人员,所以寻找任何可以检查/克服我的问题的建议。

根据要求提供一些代码细节。在检索图像的回调完成后,我正在添加我的 PageViewController。PlantPhotoContainerView 是一个静态单元格内的 UIView,它与情节提要一起添加。

self.plantPhotoContainerView.addSubview(vc.view)

pageviewcontroller 将委托设置为自身,我目前正在忽略控制器中传递的图像,只使用一些测试图像:

class ImageSliderViewController: UIPageViewController, UIPageViewControllerDataSource {
    init(images: [String]) {
        super.init(transitionStyle: UIPageViewController.TransitionStyle.scroll, navigationOrientation: UIPageViewController.NavigationOrientation.horizontal, options: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var images = ["Growers", "Home", "Media"]

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSource = self

        view.backgroundColor = UIColor.yellow

        let imageViewController = ImageViewController()
        imageViewController.imageName = images.first
        let viewControllers = [imageViewController]

        setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
    }

    override func viewDidLayoutSubviews() {
        self.view.translatesAutoresizingMaskIntoConstraints = false
        view.leftAnchor.constraint(equalTo: view.superview!.leftAnchor).isActive = true
        view.rightAnchor.constraint(equalTo: view.superview!.rightAnchor).isActive = true
        view.topAnchor.constraint(equalTo: view.superview!.topAnchor).isActive = true
        view.bottomAnchor.constraint(equalTo: view.superview!.bottomAnchor).isActive = true
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentImageName = (viewController as! ImageViewController).imageName!
        let currentIndex = images.firstIndex(of: currentImageName)!

        if currentIndex < images.count - 1 {
            let imageViewController = ImageViewController()
            imageViewController.imageName = images[currentIndex + 1]

            return imageViewController
        }

        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentImageName = (viewController as! ImageViewController).imageName!
        let currentIndex = images.firstIndex(of: currentImageName)!

        if currentIndex > 0 {
            let imageViewController = ImageViewController()
            imageViewController.imageName = images[currentIndex - 1]

            return imageViewController
        }

        return nil
    }
}

标签: iosuipageviewcontroller

解决方案


好的,我最终找到了一个有帮助的帖子。添加子视图时,需要额外的两行才能将默认手势功能传递到 pageviewcontroller。希望这对其他人有帮助。

self.addChild(vc)
self.plantPhotoContainerView.addSubview(vc.view)
vc.didMove(toParent: self)

老实说,我不完全理解这是做什么的,也许更精通 swift 框架的人可以详细说明。


推荐阅读