滚动 UIScrollView 期间 UILabel 更新停止 [英] UILabel updating stops during scrolling UIScrollView

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

问题描述

我有一个带有 imageView 的 scrollView.scrollView是superView的子View,imageView是scrollView的子View.我还有一个标签(在超级视图级别),它每毫秒从 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 值按预期更新,但在滚动时,更新显示在某处被覆盖.无论是否滚动,我都希望在标签上显示更新.

The problem is: During scrolling, the label stops to display the updates. When the scrolling is end, updates on the label restart. When updates restart they are correct; this means that label.text values are updated as expected, but while scrolling, updates display is overriden somewhere. I would like to display updates on the label regardless of scrolling or not.

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

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

感谢您的帮助.

推荐答案

我最近遇到了同样的问题,并在这里找到了解决方案:我的自定义 UI 元素....

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 更新停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆