我正在尝试在文本标签中显示带有等待指示器的等待状态。
-(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);
}
我的实现是否有问题,或者任何人都可以在代码运行时为等待指示器显示提供另一个代码。提前致谢。