UIActivityIndi​​catorView动画延迟了

时间:2013-07-08 15:39:36

标签: iphone objective-c uiactivityindicatorview

我处理的应用程序有一些繁重的任务(Ajax请求和JSON解析),我想使用UIIndicatorViews来显示设备正忙。

假设我从“源”视图开始,并希望在加载数据后进入“目标”视图之前显示指示符。

我的方法:在source.didSelectRowAtPath中启动指标,在target.viewDidLoad中加载数据,停止source.viewDidDisappear中的指标。

问题:指示器仅在延迟后动画。

“Source.m”

- (void)startIndicator {
   indicator.hidden = NO;
   [indicator startAnimating];
}

- (void)stopIndicator {
   indicator.hidden = YES;
   [indicator stopAnimating];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [self stopIndicator];
}

- (void)viewDidDisappear:(BOOL)animated {
   [self stopIndicator];
   [super viewDidDisappear:animated];
}

“目标”

- (void)viewDidLoad {
   [super viewDidLoad];
   [self longLoadingMethod];
}

2 个答案:

答案 0 :(得分:4)

我只需使用 detachNewThreadSelector 在另一个帖子中启动指标!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [NSThread detachNewThreadSelector:@selector(stopIndicator)
                            toTarget:self
                          withObject:nil];
   // instead of [self stopIndicator];
}

修改

并开始指标:

[NSThread detachNewThreadSelector:@selector(startIndicator)
                            toTarget:self
                          withObject:nil];

// instead of [self startIndicator];

答案 1 :(得分:0)

为什么不触发用户操作的加载(触发转换的操作)?

  1. 用户点击UIResponder加载从后台开始(当前VC是a 委托或使用完成块)
  2. 活动指示器已激活 (确保在主线程上发生这种情况)
  3. 加载完成后, 隐藏活动指标,过渡视图
  4. 这一切都可以在不搞乱NSThread的情况下完成 - 如果你加载失败也许你不想过渡视图(也许是为了显示警告?)。

    修改

    你可以这样做:

    - (IBAction)didTapLoadResource:(id)sender
    {
        [self startIndicator];
        [NSURLConnection sendAsynchronousRequest:self.remoteResource queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
            [self stopIndicator];
            if (error)
            {
                [self showFailureAlert];
            }
            else
            {
                [self presentViewController:self.loadedVC animated:YES completion:nil];
            }
        }];
    }
    

    请注意,此代码是在浏览器中编写的,可能需要编辑。