代码之家  ›  专栏  ›  技术社区  ›  Crocobag

UIView animateKeyframes不使用NSLayoutConstraints

  •  0
  • Crocobag  · 技术社区  · 7 年前

    UIView.animateKeyFrames . 这是我的密码:

        UIView.animateKeyframes(withDuration: 2.0, delay: 0, options: [.calculationModeCubic], animations: {
            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1.0/2.0, animations: {
                self.titleLabel.alpha = 0.0 // works well
            })
    
           self.titleSquareHeightConstraint?.constant = 20 // previously 170
            UIView.addKeyframe(withRelativeStartTime: 1.0/2.0, relativeDuration: 1.0/2.0, animations: {
                self.titleSquare.layoutIfNeeded() // instantaneously jump to 20
            })
    
        }, completion:nil)
    

    标题栏的高度会瞬间从170跳到20。 注意:titleSquare是一个UIView . 还有,我以前和 UIView.animate 下面的代码工作正常:

        self.titleSquareHeightConstraint?.constant = 20
        UIView.animate(withDuration: 0.5,
                       delay: 0,
                       usingSpringWithDamping: 1,
                       initialSpringVelocity: 1,
                       options: .curveEaseIn,
                       animations: {
    
                        self.view.layoutIfNeeded() // Perfectly works
    
        },completion: nil)
    

    我的电脑出了什么问题 UIView.animateKeyframes

    谢谢

    1 回复  |  直到 7 年前
        1
  •  3
  •   DonMag    7 年前

    直观的或不完全的,但。。。

    看法 layoutIfNeeded()

        UIView.animateKeyframes(withDuration: 2.0, delay: 0, options: [.calculationModeCubic], animations: {
    
            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1.0/2.0, animations: {
                self.titleLabel.alpha = 0.0 // works well
            })
    
            self.titleSquareHeightConstraint.constant = 20 // previously 170
    
            UIView.addKeyframe(withRelativeStartTime: 1.0/2.0, relativeDuration: 1.0/2.0, animations: {
    
                // tell self.view to layout, not the titleSquare view
                self.view.layoutIfNeeded()
    
            })
    
        }, completion:nil)