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

标签 iphone objective-c ios uiviewcontroller uiscrollview

我有一个 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/32973489/

相关文章:

iphone - 是否有可能在 iOS 上获得列出的注册电子邮件帐户?

ios - 显示 App Store 应用程序,而不是 iTunes 音乐,由给定的开发人员与所有操作系统版本的相同链接?

iOS:iAd 单例混淆

iPhone - 使用 CALayers 而不是 UIView

ios - sceneKit 物理主体的比例不符合附加节点

iphone - 将 UIView 添加到另一个是否安全,以便 subview 的框架比父 View 的框架大得多

objective-c - NSNumber 字面量

iOS - 创建启用授权的 "white label"iOS 应用涉及什么?

ios - 如何在 iOS 项目中使用 LibXtract?

iphone - Apple 为应用程序内购买提供什么样的内容,在什么条件下?