0

我正在尝试在文本标签中显示带有等待指示器的等待状态。

-(void)progressTask {

    while (taskInProgress == YES) {

        if (progressStepChanged == YES) {
            progressStepChanged = NO;
            switch (progressStep) {
                case E_PROGRESS_NONE:
                    break;

                case E_PROGRESS_WAIT:
                    HUD.mode = ProgressHUDModeIndeterminate;
                    HUD.labelText = @"Please Wait";
                    HUD.detailsLabelText = @"Transaction in Progress";
                    HUD.labelTextColor = [UIColor whiteColor];
                    HUD.detailsLabelTextColor = [UIColor whiteColor];
                    HUD.dimBackground = YES;
                    break;

                case E_PROGRESS_DECLINED:
                    HUD.mode = ProgressHUDModeCustomView;
                    HUD.labelText = @"Transaction Result";
                    HUD.detailsLabelText = @"Declined";
                    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Cross.png"]];
                    HUD.labelTextColor = [UIColor blueColor];
                    HUD.detailsLabelTextColor = [UIColor redColor];
                    HUD.dimBackground = YES;
                    break;

                case E_PROGRESS_COMM_LOST:
                    HUD.dimBackground = YES;
                    break;

                case E_PROGRESS_APPROVED:
                    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                    HUD.mode = ProgressHUDModeCustomView;
                    HUD.labelText = @"Transaction Result";
                    HUD.detailsLabelText = @"Approved";
                    HUD.labelTextColor = [UIColor blueColor];
                    HUD.detailsLabelTextColor = [UIColor greenColor];
                    HUD.dimBackground = YES;
                    break;

                default:
                    break;
            }
        }
        usleep(100);
    }
}

-(void)startProgressManager:(UIViewController*)viewController {
    if (HUD == nil) {
        HUD = [[ProgressHUD alloc] initWithView:viewController.navigationController.view];
        [viewController.navigationController.view addSubview:HUD];
        HUD.delegate = self;
    }
    [HUD showWhileExecuting:@selector(progressTask) onTarget:self withObject:nil animated:YES];
}

“progressTask”中的每个参数都有一个观察者。ProgressHUD 类负责在信息文本中创建等待指示器。“progressTask”作为后台任务启动:

-(void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated {
    methodForExecution = method;
    targetForExecution = target;
    objectForExecution = object;    
    // Launch execution in new thread
    self.taskInProgress = YES;
    [NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil];
    // Show HUD view
    [self show:animated];
}


-(void)show:(BOOL)animated {

    if (animated && animationType == ProgressHUDAnimationZoomIn) {
        self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f));
    } else if (animated && animationType == ProgressHUDAnimationZoomOut) {
        self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f));
    }
    self.showStarted = [NSDate date];
    // Fade in
    if (animated) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.30];
        self.alpha = 1.0f;
        if (animationType == ProgressHUDAnimationZoomIn || animationType == ProgressHUDAnimationZoomOut){
            self.transform = rotationTransform;
        }
        [UIView commitAnimations];
        [self setNeedsDisplay];
    }
    else {
        self.alpha = 1.0f;
    }
}

问题是当我从 ViewController 类调用“startProgressManager”时,等待指示器仅在方法退出后(在 sleep(3) 之后)显示。

-(IBAction)recallBtnPress:(id)sender {
    progressManager = [ProgressManager new];
    [progressManager startProgressManager:self];
    progressManager.progressStep = E_PROGRESS_WAIT;
    sleep(3);
}

我的实现是否有问题,或者任何人都可以在代码运行时为等待指示器显示提供另一个代码。提前致谢。

4

1 回答 1

1

不要在主线程上休眠,也不要在主线程之外更新 UI 组件。

一般模式是切换到后台任务的单独执行线程,当需要更新时切换到主线程来更新 UI。

NSThread只是出于好奇,您使用而不是Grand Central Dispatch是否有原因?


更新

这是一个非常基本的切换到后台线程,然后切换到主线程的场景。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // THIS CODE RUNS IN THE BACKGROUND

    // Your data crunching. You can call methods on self, if it's thread safe.

    // AFTER PROCESSING
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        // SWITCH BACK TO THE MAIN THREAD

        // Update the UI.
    });
});
于 2013-04-19T11:37:44.587 回答