gpt4 book ai didi

ios - 更改 UILabel 文本导致 scrollview 跳转

转载 作者:搜寻专家 更新时间:2023-11-01 07:13:20 25 4
gpt4 key购买 nike

我正在使用计时器来更新 UILabel 的文本属性。 UILabel 在垂直的 UIScrollView 中。

static func play() {
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.timerUpdate), userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: .commonModes)
}

@objc private static func timerUpdate(_ timer: Timer) {
let progress = AudioManager.currentTime
NotificationCenter.default.post(name: NSNotification.Name("didUpdateProgress"), object: self, userInfo: ["progress":progress])
}

当调用下一个函数时,currentTimeLabel.text 会更新为新字符串。

func didUpdateProgress(_ notification: Notification) {
if let progress = notification.userInfo?["progress"] as? Float {
slider.setValue(progress, animated: true)
currentTimeLabel.text = String(format: "%01i:%02i", Int(progress/60), Int(progress) % 60)
}
}

但是如果scrollview不在初始位置,即contentOffset不为0,每次currentTimeLabel.text变化,scrollView都会跳起来。我想,为这个特定标签显式设置高度约束将解决这个问题,但它没有:(

我该怎么做才能用计时器更新我在 UIScrollView 中的 UILabel 文本,这样 ScrollView 就不会跳转了?

最佳答案

我所描述的问题是在我的 viewDidLayoutSubviews() 方法中。每次 View 出现在屏幕上时,我都希望 ScrollView 重置其位置。所以我这样做了

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.setContentOffset(.zero, animated: false)
}

我不知道每次更改标签的文本属性时都会调用此方法。好吧,现在看起来完全清楚了,因为更改文本会使标签重新计算其宽度,但一开始并没有那么明显:)

抱歉,我错过了问题中的这一部分。尽量避免不必要的细节并尽量减少问题,很容易忽略一些重要的事情,尤其是当您真的不知道问题出在哪里时。

关于ios - 更改 UILabel 文本导致 scrollview 跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43665240/

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