从pageViewController

时间:2017-01-31 19:30:08

标签: ios swift xcode segue uipageviewcontroller

我的问题如下:我正在使用pageViewControllers为我正在创建的应用程序创建教程/入职页面。在我的教程的最后一页(第3页)上的左滑动手势应该执行一个segue。

这就是我现在所拥有的,但它给了我一个关键值编码 - 合规性错误。我有两次,三次检查我的网点,swipeView非常正确地连接。

class GrowController: UIViewController {

@IBOutlet weak var swipeView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(swipeView)
    createGesture()

}

func createGesture() {
    let showReg = UISwipeGestureRecognizer(target: self, action: #selector(showRegister))
    showReg.direction = .left
    self.swipeView.addGestureRecognizer(showReg)
}

func showRegister(gesture: UISwipeGestureRecognizer) {
    performSegue(withIdentifier: "showRegister", sender: self)
  }
}

这是实际UIViewController(正在显示的特定页面)的控制器

现在我也试过在我的TutorialViewController中弄乱一些逻辑,这是UIPageViewController控制滑动表单页面到页面等。

这里的逻辑

class TutorialViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

var viewControllerIndex: Int?

//Array of my pages to load (GrowController is page3)
lazy var tutorialArray: [UIViewController] = {
    return [self.tutorialInstance(name: "page1"), self.tutorialInstance(name: "page2"), self.tutorialInstance(name: "page3")]
}()

private func tutorialInstance(name: String?) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name!)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self
    self.delegate = self

    if let firstViewController = tutorialArray.first {
        setViewControllers([firstViewController], direction: .forward, animated: false, completion: nil)
    }
}

// Scroll view
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds 
        }
        else if view is UIPageControl {
            view.backgroundColor = UIColor.clear
        }
    }
}


// Page View Controller delegate functions
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = tutorialArray.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
            return nil
    }

    guard tutorialArray.count > previousIndex else {
// Added this line just testing around, nothing happened here though.
        performSegue(withIdentifier: "ShowRegister", sender: self)
        return nil
    }

    return tutorialArray[previousIndex]
}


public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = tutorialArray.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < tutorialArray.count else {
        return nil
    }

    guard tutorialArray.count > nextIndex else {
        return nil
    }

    return tutorialArray[nextIndex]
}

public func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return tutorialArray.count
}

public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = tutorialArray.index(of: firstViewController) else {
        return 0
    }
    return firstViewControllerIndex
    }


}

是否与willTransitionTo pageView委托方法有关?我不熟悉如何实现,我已经尝试过了。

我认为我可以向Grow控制器添加一个subView,在其中放置一个滑动手势,并在用户从该页面向左滑动时执行segue。截至目前,此代码在加载page3(GrowController

时崩溃了应用程序

任何帮助非常感谢,我一直试图解决这个问题超过一周,这是我提出的第二个问题。谢谢!

enter image description here

1 个答案:

答案 0 :(得分:0)

不要使用UIPageViewController。这非常具有讽刺意味,因为我通常是第一个建议使用UIPageViewController的人。但在这种情况下,它为您做了太多的工作,并且您不能以这样的方式干涉,以按照您描述的方式对其进行自定义。您将需要使用简单的分页UIScrollView;这样,你将完全负责所发生的事情,并通过滚动视图的委托获得细粒度的响应,并且您可以访问底层的平移手势识别器并且可以修改它的行为。