gpt4 book ai didi

ios - 添加文本时 UILabel 反弹

转载 作者:可可西里 更新时间:2023-11-01 01:06:39 26 4
gpt4 key购买 nike

文本以编程方式添加到 UILabel。随着添加更多文本,文本换行并增加标签的高度。

问题是,当文本在一行的末尾换行时,整个标签将跳起 1 行的高度并自行动画回到正确的位置。最终结果很好,但是你如何摆脱奇怪的跳跃动画。同样,每次 UILabel 的高度由于另一个换行而增加时都会发生这种情况。

示例代码:

var eventCount = 0;
func someEvent(sender:AnyObject){
eventCount += 1;
if(eventCount == 1){
lbl.text = "this"
}else if(eventCount == 2){
lbl.text = "this is some"
}else if(eventCount == 3){
lbl.text = "this is some sample text"
}else if(eventCount == 4){
// this is where text wraps to line 2
// the label jumps up 20px or so and
// animates back down to it's original position
lbl.text = "this is some sample text that causes the label to wrap"
}
}

自动布局约束

            0
|
0 - UILabel - 0

标签属性

lines = 0

最佳答案

我已经成功地用下面的代码重现了这个问题:

class ViewController: UIViewController {

@IBOutlet weak var label: UILabel!

@IBAction func buttonPushed(sender: AnyObject) {
UIView.animateWithDuration(0.5) {
self.someEvent(self)
self.view.layoutIfNeeded()
}
}

func someEvent(sender:AnyObject){
self.label.text! += " test"
}
}

所以,我相信您的 someEvent() 是在动画 block 中调用的。

UIView.performWithoutAnimation 解决了这个问题。

var eventCount = 0;
func someEvent(sender:AnyObject){
UIView.performWithoutAnimation {
self.eventCount += 1;
if(self.eventCount == 1){
self.lbl.text = "this"
}else if(self.eventCount == 2){
self.lbl.text = "this is some"
}else if(self.eventCount == 3){
self.lbl.text = "this is some sample text"
}else if(self.eventCount == 4){
self.lbl.text = "this is some sample text that causes the label to wrap"
}
self.view.layoutIfNeeded()
}
}

关于ios - 添加文本时 UILabel 反弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26661927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com