CGAffineTransform 不在动画块内设置动画

CGAffineTransform not animating within animation block

我试图让一个视图在它出现时用 spring 增加它的比例(使用下面的 loadResultsView() 函数),但是每当我 运行 这个,没有动画发生。视图以动画块中定义的结果状态结束。我尝试使用帧值而不是对视图的约束,但同样的事情发生了。为什么会这样?

class ResultsModalViewController: UIViewController {

    let resultsView: ResultsView = {
        let nibArray = Bundle.main.loadNibNamed("ResultsView", owner: self, options: nil)
        let resultsView = nibArray?.first as! ResultsView

        resultsView.restaurant = YelpSearchController.shared.pickRandomRestaurant()

        resultsView.translatesAutoresizingMaskIntoConstraints = false

        resultsView.layer.cornerRadius = 5

        return resultsView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.30)

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(bgViewTapped))
        view.addGestureRecognizer(tapGestureRecognizer)


        loadResultsView()
    }

    func loadResultsView() {

        self.view.addSubview(resultsView)

        self.resultsView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)

        UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            self.resultsView.transform = CGAffineTransform.identity
        })

        NSLayoutConstraint.activate([
            resultsView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            resultsView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
            resultsView.widthAnchor.constraint(equalToConstant: 300)
            ])
    }

    @objc func bgViewTapped() {
        dismiss(animated: true, completion: nil)
    }

}

首先你需要添加一个高度限制

resultsView.heightAnchor.constraint(equalToConstant: 300)

然后在

中制作动画
override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.30)
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(bgViewTapped))
    view.addGestureRecognizer(tapGestureRecognizer)
    setupResultsView()
}
override func viewDidAppear(_ animated:Bool) {
   super.viewDidAppear(animated)
   animateResultsView()
}

func setupResultsView() {
    self.view.addSubview(resultsView)
    NSLayoutConstraint.activate([
        resultsView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
        resultsView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
        resultsView.widthAnchor.constraint(equalToConstant: 300),
        resultsView.heightAnchor.constraint(equalToConstant: 300)
        ])
}
func animateResultsView() {
    self.resultsView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)            
    UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseOut, animations: {          
        self.resultsView.transform = CGAffineTransform.identity
    })
}

通过将 loadResultsView() 而不是 viewDidLoad() 放入 viewWillAppear() 方法中解决了这个问题。感谢@Koen 指出 viewDidLoad() 在视图出现之前被调用。

class ResultsModalViewController: UIViewController {

    let resultsView: ResultsView = {
        let nibArray = Bundle.main.loadNibNamed("ResultsView", owner: self, options: nil)
        let resultsView = nibArray?.first as! ResultsView

        resultsView.restaurant = YelpSearchController.shared.pickRandomRestaurant()

        resultsView.translatesAutoresizingMaskIntoConstraints = false

        resultsView.layer.cornerRadius = 5

        return resultsView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(bgViewTapped))
        view.addGestureRecognizer(tapGestureRecognizer)

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        view.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.30)
        loadResultsView()
    }

    func loadResultsView() {

        self.view.addSubview(resultsView)

        self.resultsView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)

        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            self.resultsView.transform = CGAffineTransform.identity
        })

        NSLayoutConstraint.activate([
            resultsView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            resultsView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
            resultsView.widthAnchor.constraint(equalToConstant: 300)
            ])
    }

    @objc func bgViewTapped() {
        dismiss(animated: true, completion: nil)
    }

}