【发布时间】:2017-04-27 18:17:37
【问题描述】:
我正在使用计时器来更新 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不为零,那么每次currentTimeLabel.text变化时,scrollView都会向上跳。我想,为这个特定标签显式设置高度约束会解决这个问题,但它没有:(
如何使用计时器更新 UIScrollView 中的 UILabel 文本,以使滚动视图不会跳转?
【问题讨论】:
-
首先,您确定这是由设置标签文本引起的,而不是为滑块设置动画?
-
@DonMag 评论了更新 currentTimeLabel 修复跳跃的行
-
hmmm...必须在您的代码中发生其他事情...如果您注释掉 slider.setValue 行会发生什么?
-
@DonMag 滑块只是停止更新,但如果我不注释掉更新 currentTimeLabel =[,滚动视图仍会在其初始位置跳转
-
您是否使用子分类标签在设置文本时执行其他操作?或者还有什么事情发生?显然,在 UIScrollView 中设置 UILabel 的文本导致滚动视图跳转是 not 设计的......
标签: ios swift uiscrollview uilabel