Are my pageviewcontroller gestures being intercepted?

时间:2019-04-16 23:16:56

标签: ios uipageviewcontroller

I have followed a pageviewcontroller tutorial to create an image slider. I have then adapted it to insert into a tableviewcell as I want an image slider within the table.

I like the idea of using this same code in full screen or embedded into another view.

However, when I do this, the pageviewafter and pageviewbefore events dont fire when sliding. I suspect the tableviewcontroller or cell are gobbling up the events so the pageviewcontroller isnt receiving them.

Im a part time ios developer at best, so looking for any suggestions I can check out/overcome my problem.

Some code detail as requested. I am adding my PageViewController after a callback of retreiving the images completes. plantPhotoContainerView is a UIView inside a static cell which was added with the storyboard.

self.plantPhotoContainerView.addSubview(vc.view)

The pageviewcontroller sets the delegate to itself and I'm currently ignoring the images passed in the controller and just using some test images:

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
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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