对UIPageViewController

时间:2019-05-10 13:41:21

标签: ios swift

我正在尝试用performSegue()的动画从左开始:

class SegueFromLeft: UIStoryboardSegue {
    override func perform() {
        let src = self.source
        let dst = self.destination

        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransform(translationX: -src.view.frame.size.width, y: 0)

        UIView.animate(withDuration: 0.25,
                       delay: 0.0,
                       options: .curveEaseInOut,
                       animations: {
                        dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
        },
                       completion: { finished in
                        src.present(dst, animated: false, completion: nil)
        }
        )
    }

但是以某种方式,在执行src.present(dst, animated: false, completion: nil)时,此行出现以下错误:performSegue

  

线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x16b607fc0)

我可以看到动画,但是一旦显示新视图,应用就会崩溃。

PageViewController,以帮助解决问题:

class LeasingTutorialViewController: UIPageViewController, UIPageViewControllerDataSource  {

    lazy var viewControllerList:[UIViewController] = {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let vc1 = sb.instantiateViewController(withIdentifier: "leasingPageOne")
        let vc2 = sb.instantiateViewController(withIdentifier: "leasingPageTwo")

        return [vc1, vc2]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        if let firstViewController = viewControllerList.first as? LeasingPageOneViewController {
            self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
        }
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else { return nil }

        guard viewControllerList.count > previousIndex else { return nil }

        return viewControllerList[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let nextIndex = vcIndex + 1

        guard viewControllerList.count != nextIndex else { return nil }

        guard viewControllerList.count > nextIndex else { return nil }

        return viewControllerList[nextIndex]
    }
}

编辑:

在某些情况下,它是在其他ViewController(“ goToLeasingTutorial”)中调用的:

另一个重要说明:重新启动应用程序后,它又可以正常工作。

@IBAction func goToLeasing(_ sender: Any) {
        if leasingTutorialSeen == false && leasingInStartScreen == true {
            performSegue(withIdentifier: "goToLeasingTutorial", sender: self)
        } else if leasingTutorialSeen == true && leasingInStartScreen == true {
            buttonHamburgerTapped(self)

            let parentVC = self.parent as! RootPageViewController
            parentVC.setViewControllers([parentVC.viewControllerList[1]], direction: .forward, animated: true, completion: nil)
        } else if leasingTutorialSeen == true && leasingInStartScreen == false {
            performSegue(withIdentifier: "goToLeasing", sender: self)
        }
    }

0 个答案:

没有答案