如果动画同时启动,则UIPageViewController转换错误

时间:2017-10-28 09:54:22

标签: ios swift uipageviewcontroller

我的应用中使用UIPageViewController有一种奇怪的行为。 我的应用程序的布局是一个PageViewController(像相机卷),底部有一个广告横幅。

横幅广告素材的容器以隐藏方式启动,当广告加载后,我会设置isHidden=false动画。

我的问题是,当横幅进入屏幕时,如果正在进行中,它会破坏UIPageViewController转换,如此视频所示:

enter image description here

我创建了一个新项目,可以通过几行简单地重现错误,您可以在GITHUB结帐:您只需要垃圾邮件"下一步"按钮,直到横幅加载。它也可以通过翻转PageViewController来复制,但更难重现。

完整的示例代码是:

class TestViewController: UIViewController,UIPageViewControllerDelegate,UIPageViewControllerDataSource {
    @IBOutlet weak var constraintAdviewHeight: NSLayoutConstraint!
    weak var pageViewController : UIPageViewController?

    @IBOutlet weak var containerAdView: UIView!
    @IBOutlet weak var adView: UIView!
    @IBOutlet weak var containerPager: UIView!

    var currentIndex = 0;
    var clickEnabled = true

    override func viewDidLoad() {
        super.viewDidLoad()
        let pageVC = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        pageViewController = pageVC
        pageVC.delegate = self
        pageVC.dataSource = self
        addChildViewController(pageVC)
        pageVC.didMove(toParentViewController: self)
        containerPager.addSubview(pageVC.view)
        pageVC.view.translatesAutoresizingMaskIntoConstraints = true
        pageVC.view.frame = containerPager.bounds
        pushViewControllerForCurrentIndex()
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.containerAdView.isHidden = true

        DispatchQueue.main.asyncAfter(deadline: .now()+4) {
            self.simulateBannerLoad()
        }

    }

    @IBAction func buttonClicked(_ sender: Any) {
        guard clickEnabled else {return}
        currentIndex -= 1;
        pushViewControllerForCurrentIndex()

    }

    @IBAction func button2Clicked(_ sender: Any) {
        guard clickEnabled else {return}
        currentIndex += 1;
        pushViewControllerForCurrentIndex()
    }


    private func simulateBannerLoad(){
        constraintAdviewHeight.constant = 50
        pageViewController?.view.setNeedsLayout()
        UIView.animate(withDuration: 0.3,
                       delay: 0, options: .allowUserInteraction, animations: {
                        self.containerAdView.isHidden = false
                        self.view.layoutIfNeeded()
                        self.pageViewController?.view.layoutIfNeeded()
        })

    }

    //MARK: data source

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        return getViewControllerForIndex(currentIndex+1)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        return getViewControllerForIndex(currentIndex-1)
    }

    func getViewControllerForIndex(_ index:Int) -> UIViewController? {
        guard (index>=0) else {return nil}
        let vc :UIViewController = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "pageTest")
        vc.view.backgroundColor = (index % 2 == 0) ? .red : .green
        return vc
    }

    func pushViewControllerForCurrentIndex() {
        guard let vc = getViewControllerForIndex(currentIndex) else {return}
        print("settingViewControllers start")
        clickEnabled = false
        pageViewController?.setViewControllers([vc], direction: .forward, animated: true, completion: { finished in
            print("setViewControllers finished")
            self.clickEnabled = true
        })
    }
}

注意:另一个不受欢迎的影响是,当错误发生时,最后一个完成阻止不会被调用,因此它会禁用按钮:

    func pushViewControllerForCurrentIndex() {
        guard let vc = getViewControllerForIndex(currentIndex) else {return}
        print("settingViewControllers start")
        clickEnabled = false
        pageViewController?.setViewControllers([vc], direction: .forward, animated: true, completion: { finished in
            print("setViewControllers finished")
            self.clickEnabled = true
        })
    }

Note2 :横幅加载事件是我无法手动控制的事情。由于用于显示广告的库,它在主线程中的回调可能在任何时刻发生。在示例proyect中,这是使用DispatchQueue.main.asyncAfter:调用

进行模拟的

我该如何解决?感谢

1 个答案:

答案 0 :(得分:2)

有什么问题?

布局会中断动画。

如何预防?

在pageViewController动画时不更改布局。

加载广告时: 确认pageViewController是否为动画,如果是,请等待动画完成然后更新或更新

<强>示例:

    private func simulateBannerLoad(){
        if clickEnabled {
            self.constraintAdviewHeight.constant = 50
        } else {
            needUpdateConstraint = true
        }
    }
    var needUpdateConstraint = false
    var clickEnabled = true {
        didSet {
            if clickEnabled && needUpdateConstraint {
                self.constraintAdviewHeight.constant = 50
                needUpdateConstraint = false
            }
        }
    }