【问题标题】:CABasicAnimation Sometimes Works, Sometimes FailsCABasicAnimation 有时有效,有时失败
【发布时间】:2019-09-03 19:40:02
【问题描述】:

我正在制作一个应用程序,通过一系列步骤引导用户创建帐户。完成每个步骤后,用户将被带到下一个视图控制器,并且屏幕顶部会出现一个进度条,以传达已完成的帐户创建过程的多少。这是最终结果:

这是通过在包含视图中放置导航控制器来实现的。进度条覆盖在包含视图上,每次导航控制器推送一个新的视图控制器时,它都会告诉包含视图控制器将进度条设置为父视图宽度的某个百分比。这是通过以下updateProgressBar 函数完成的。

import UIKit

class ContainerVC: UIViewController {
    var progressBar: CAShapeLayer!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        progressBar = CAShapeLayer()
        progressBar.bounds = CGRect(x: 0, y: 0, width: 0, height: view.safeAreaInsets.top)
        progressBar.position = CGPoint(x: 0, y: 0)
        progressBar.anchorPoint = CGPoint(x: 0, y: 0)
        progressBar.backgroundColor = UIColor.secondaryColor.cgColor
        view.layer.addSublayer(progressBar)
    }

    func updateProgressBar(to percentAsDecimal: CGFloat!) {
        let newWidth = view.bounds.width * percentAsDecimal
        CATransaction.begin()
        CATransaction.setCompletionBlock({
            self.progressBar.bounds.size.width = newWidth
        })
        CATransaction.commit()
        let anim = CABasicAnimation(keyPath: "bounds")
        anim.isRemovedOnCompletion = true
        anim.duration = 0.25
        anim.fromValue = NSValue(cgRect: CGRect(x: 0, y: 0, width: progressBar.bounds.width, height: view.safeAreaInsets.top))
        anim.toValue = NSValue(cgRect: CGRect(x: 0, y: 0, width: newWidth, height: view.safeAreaInsets.top))
        progressBar.add(anim, forKey: "anim")
    }
}

导航控制器堆栈中的视图控制器将在推送下一个 VC 时调用此 updateProgressBar 函数。这样做是这样的:

class FourthViewController: UIViewController {

    var containerVC: ContainerViewController!

    ...

    @IBAction func nextButtonPressed(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: .main)
        let fifthVC = storyboard.instantiateViewController(withIdentifier: "FifthVC") as! FifthViewController
        fifthVC.containerVC = containerVC
        navigationController!.pushViewController(fifthVC, animated: true)
        //We pass 5/11 because the next step will be step 5 out of 11 total steps
        self.containerVC.updateProgressBar(to: 5/11)
    }
}

同理,当按下返回键时,我们收缩容器VC的进度条:

class FourthViewController: UIViewController {

    var containerVC: ContainerViewController!

    ...

    @IBAction func backButtonPressed(_ sender: Any) {
        navigationController!.popViewController(animated: true)

        //We pass 3/11 because the previous step is step 3 out of 11 total steps
        containerVC.updateProgressBar(to: 3/11)
    }
}

我的问题是这个动画有时有效。进度条在进程中向前移动时始终有效,但有时,当用户向后导航时,进度条会卡住并且不会再向任一方向移动,直到出现未到达的视图控制器。请看下面的视频:

Video of Bug (Bug begins around 0:23)

我已确认警报控制器的显示不是动画失败的原因,并且还确保动画正在主线程上发生。有什么建议吗?

【问题讨论】:

    标签: ios swift core-animation cabasicanimation


    【解决方案1】:

    正如这个答案here 中很好解释的那样,viewDidLayoutSubviews() 被多次调用。

    在您的情况下,每次您从导航堆栈中推送或弹出视图控制器时,您最终都会创建一个新的 CAShapeLayer。

    尝试改用viewDidAppear()。

    【讨论】:

    • 我很惊讶我竟然这么愚蠢,竟然没看到 ??‍♂️。我想有时你只需要第二双眼睛。非常感谢!
    猜你喜欢
    • 2012-10-21
    • 2020-10-12
    • 1970-01-01
    • 2012-01-17
    • 2012-09-05
    • 2014-05-01
    • 2015-07-04
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多