UIlabel动画不适合在屏幕上

UIlabel动画不适合在屏幕上

问题描述:

这是一个问答应用程序,提出问题,回答后,下一个问题在整个屏幕上呈现动画效果。问题是,如果下一个问题是一个更长的问题,它会通过屏幕。我把标签设置为2行。UIlabel动画不适合在屏幕上

func animateLabelTransitions(){ 

    // Animate the alpha 
    // and the center X constraints 
    let screenWidth = view.frame.width 
    self.nextQuestionLabelCenterXConstraint.constant = 0 
    self.currentQuestionCenterXConstraint.constant += screenWidth 
    UIView.animate(withDuration: 0.5, 
          delay: 0, 
         options: [], 
        animations: { 
           self.currentQuestionLabel.alpha = 0 
           self.nextQuestionLabel.alpha = 1 

           self.view.layoutIfNeeded() 
           }, 
        completion: { _ in 
           swap(&self.currentQuestionLabel, 
           &self.nextQuestionLabel) 
            swap(&self.currentQuestionCenterXConstraint, 
            &self.nextQuestionLabelCenterXConstraint) 

            self.updateOffScreenLabel() 

            }) 
} 

你可以尝试把这个在您的layoutSubviews方法也

self.currentQuestionLabel.preferredMaxLayoutWidth = self.view.frame.width 
+1

参见:['sizeThatFits'](https://developer.apple.com/reference/uikit/uiview/1622625- sizethatfits)。 –