我遵循了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
    }
}

最佳答案

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

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


老实说,我不完全了解它的作用,也许精通swift框架的人可以阐述一下。

关于ios - 我的pageviewcontroller手势是否被拦截?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55717801/

10-11 14:42