Ios 滚动表上的ProgressView块
我有个问题。我有一个UITableView和一个带有UIProgressView的视图,当我滚动表格时,progressview不会刷新进度值。。。只有当滚动完成时,进度才会刷新 我不知道为什么会这样。 我试图用dispatch\u async刷新进度Ios 滚动表上的ProgressView块,ios,uitableview,uiprogressview,Ios,Uitableview,Uiprogressview,我有个问题。我有一个UITableView和一个带有UIProgressView的视图,当我滚动表格时,progressview不会刷新进度值。。。只有当滚动完成时,进度才会刷新 我不知道为什么会这样。 我试图用dispatch\u async刷新进度 dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ //I don't know what i have to put
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
});
});
但是什么都没有改变…你就快到了
我已经复制了你的问题并解决了它
这是它没有修复,这是我认为您存在的问题(请注意,滚动时进度指示器不会更新):
这就是它的修复:
问题是,滚动也发生在主线程上并阻止它。要解决这个问题,你只需要对你的计时器做一个小的调整。初始化计时器后,添加以下内容:
[[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
调用中,但在主线程调用之前执行任何密集的操作