gpt4 book ai didi

iphone - UILabel 更新在滚动 UIScrollView 期间停止

转载 作者:IT王子 更新时间:2023-10-29 07:47:12 27 4
gpt4 key购买 nike

我有一个 scrollView,里面有一个 imageView。 scrollView是superView的子View,imageView是scrollView的子View。我还有一个标签(在 super View 级别)每毫秒从 NSTimer 接收其文本属性的更新值。

问题是:在滚动期间,标签停止显示更新。当滚动结束时,标签上的更新重新开始。当更新重新启动时,它们是正确的;这意味着 label.text 值按预期更新,但在滚动时,更新显示在某处被覆盖。无论滚动与否,我都想在标签上显示更新。

标签更新的实现方式如下:

- (void)startElapsedTimeTimer {

[self setStartTime:CFAbsoluteTimeGetCurrent()];
NSTimer *elapsedTimeTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(updateElapsedTimeLabel) repeats:YES];
}

- (void)updateElapsedTimeLabel {

CFTimeInterval currentTime = CFAbsoluteTimeGetCurrent();
float theTime = currentTime - startTime;

elapsedTimeLabel.text = [NSString stringWithFormat:@"%1.2f sec.", theTime];
}

感谢您的帮助。

最佳答案

我最近遇到了同样的麻烦并在这里找到了解决方案:My custom UI elements... .

简而言之:当您的 UIScrollView 正在滚动时,NSTimer 不会更新,因为运行循环以不同的模式运行(NSRunLoopCommonModes,用于跟踪事件的模式)。

解决方案是在创建后立即将您的计时器添加到 NSRunLoopModes:

NSTimer *elapsedTimeTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 
target:self
selector:@selector(updateElapsedTimeLabel)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:elapsedTimeTimer
forMode:NSRunLoopCommonModes];

(代码来自上面链接的帖子)。

关于iphone - UILabel 更新在滚动 UIScrollView 期间停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5377914/

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