我正在使用理想的解決方案來為 UILabel 的文本更改設定影片。類似于這個答案https://stackoverflow.com/a/33705634/8704900
問題是當文本長度不同時,影片不再平滑并且文本在影片之前閃爍。
視頻:https : //drive.google.com/file/d/1I89NnzjQp7TbemO-dmcbKzYUr7pM7mGk/view?usp=sharing
我的代碼如下所示:
@IBOutlet weak var label: UILabel!
var titleLabelAnimation: CATransition = {
let animation = CATransition()
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
animation.type = .push
animation.subtype = .fromTop
animation.duration = 0.5
return animation
}()
@IBAction func didTap() {
self.label.layer.add(self.titleLabelAnimation, forKey: nil)
self.label.text = "Can you see a flicker?"
self.label.sizeToFit()
}
@IBAction func didTapRev() {
self.label.layer.add(self.titleLabelAnimation, forKey: nil)
self.label.text = "Hello this is animation !!!"
self.label.sizeToFit()
}
我嘗試過 layoutIfNeeded()、sizeToFit()、在影片之前更改文本以及其他一些解決方法。似乎沒有任何作業!
uj5u.com熱心網友回復:
不確定您的示例中究竟發生了什么,因為我無法產生此結果。但是影片在很多情況下是一種痛苦,很多事情可能會產生跳躍視圖。
通過對影片有更多的控制,您可能會更幸運地發現問題或修復它。對于您的特定情況,使用快照制作影片可能已經足夠了。查看以下內容:
@IBOutlet weak var label: UILabel!
private func animateAsCustom(applyChanges: @escaping (() -> Void)) {
guard let viewToMove = label else { return }
guard let panel = viewToMove.superview else { return }
guard let snapshotView = viewToMove.snapshotView(afterScreenUpdates: true) else { return }
applyChanges()
UIView.performWithoutAnimation {
panel.addSubview(snapshotView)
snapshotView.center = viewToMove.center
viewToMove.transform = CGAffineTransform(translationX: 0.0, y: 50.0) // TODO: compute values for translation
viewToMove.alpha = 0.0
}
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn) {
viewToMove.transform = .identity
snapshotView.transform = CGAffineTransform(translationX: 0.0, y: -50.0)
snapshotView.alpha = 0.0
viewToMove.alpha = 1.0
} completion: { _ in
snapshotView.removeFromSuperview()
}
}
@IBAction func didTap() {
animateAsCustom {
self.label.numberOfLines = 1
self.label.text = "Can you see a flicker?"
self.label.textColor = .red
self.label.font = UIFont.systemFont(ofSize: 20)
}
}
@IBAction func didTapRev() {
animateAsCustom {
self.label.numberOfLines = 0
self.label.text = "Hello this\nis animation !!!"
self.label.textColor = .black
self.label.font = UIFont.systemFont(ofSize: 30)
}
}
如果您在上一個影片完成之前按下其中一個按鈕,這仍然無法解決問題。為了解決這個問題,可能需要一些額外的努力。但是現在這個解決方案可能就足夠了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/363392.html
上一篇:執行緒“main”中的例外java.lang.NoClassDefFoundError:com/mongodb/operation/ReadOperation
下一篇:如何解決雙重Lottie影片問題
