【问题标题】:ProgressView block on scroll table滚动表上的 ProgressView 块
【发布时间】:2016-03-04 20:11:17
【问题描述】:

我有一个问题。我有一个 UITableView 和一个带有 UIProgressView 的视图,当我滚动表格时,progressview 不会刷新进度值...只有当滚动完成时,进度才会刷新..

我不知道为什么会这样。 我尝试使用 dispatch_async 刷新进度

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

//I don't know what i have to put here, maybe the NSTimer??

    dispatch_async(dispatch_get_main_queue(), ^(void){

        //here i set the value of the progress
    });

});

但没有任何改变......

【问题讨论】:

    标签: ios uitableview uiprogressview


    【解决方案1】:

    你快到了!

    我已经复制了您的问题并修复了它。

    这是没有修复的问题,这是我认为您遇到的问题(请注意,滚动时进度指示器不会更新):

    这就是修复:

    问题是滚动也发生在主线程上并阻塞它。要解决此问题,您只需对计时器进行小幅调整。初始化计时器后,添加以下内容:

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    

    以下是一些示例最小代码:

    -(instancetype)init{
        self = [super init];
        if (self) {
            _progressSoFar = 0.0;
        }
        return self;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.progressIndicator.progress = 0.0;
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(callAfterSomeTime:) userInfo: nil repeats: YES];
        [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];
        [self.myTimer fire];
    }
    
    -(void)callAfterSomeTime:(NSTimer *)timer {
        if (self.progressSoFar == 1.0) {
            [self.myTimer invalidate];
            return;
        }
    
        // Do anything intensive on a background thread (probably not needed)
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            // Update the progress on the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                self.progressSoFar += 0.01;
                self.progressIndicator.progress = self.progressSoFar;
            });
        });
    }
    

    滚动发生在UITrackingRunLoopMode。您需要确保您的计时器也处于该模式。除非您进行一些花哨的计算,否则您不应该需要任何后台线程的东西,但我已将其包括在内以防万一。只需在 global_queue 调用中但在主线程调用之前做任何密集的事情。

    【讨论】:

      猜你喜欢
      • 2023-02-13
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多