在滚动UIScrollView期间

在滚动UIScrollView期间

本文介绍了在滚动UIScrollView期间,UILabel更新停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有imageView的scrollView。 scrollView 是superView的子视图,而imageView是 scrollView 的子视图。
我还有一个标签(在超级视图级别),每隔毫秒从NSTimer接收其文本属性的更新值。

I have a scrollView with an imageView inside of it. The scrollView is a subView of the superView, and the imageView is a subView of the scrollView.I also have a label (at the super-view level) that receives updated values on its text property from a NSTimer every millisecond.

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

以下是标签更新的实施方式:

Here is how the label updates are implemented:

- (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];
}

感谢您的帮助。

推荐答案

我最近遇到了同样的问题并在此处找到了解决方案:。

I had recently the same trouble and found the solution here: My custom UI elements....

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

In short: while your UIScrollView is scrolling, the NSTimer is not updated because the run loops run in a different mode (NSRunLoopCommonModes, mode used for tracking events).

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

The solution is adding your timer to the NSRunLoopModes just after creation:

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

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

(The code comes from the post linked above).

这篇关于在滚动UIScrollView期间,UILabel更新停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:39