这是一个测验应用程序,询问问题,并在回答后,屏幕上的下一个问题动画。问题是,如果下一个问题是一个较长的问题,它将超过屏幕。我将标签设置为2行。
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()
})
}
答案 0 :(得分:1)
您可以尝试将其放入layoutSubviews
方法
self.currentQuestionLabel.preferredMaxLayoutWidth = self.view.frame.width