在背景触摸上关闭UIActivityIndi​​catorView

时间:2013-11-09 12:44:18

标签: ios objective-c cocoa-touch uiactivityindicatorview

我从网络服务获取数据,在数据提取期间,我在屏幕上显示UIAcitivityIndicatorView。该指标位于UIAlertView

UIAlertView *waitAlert = [[UIAlertView alloc] initWithTitle:@"Please Wait...." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];

    [waitAlert show];

indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(waitAlert.bounds.size.width / 2, waitAlert.bounds.size.height - 50);
[waitAlert addSubview:indicator];

NSURL *url = [NSURL URLWithString:URLString];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSThread detachNewThreadSelector:@selector(startAnimation) toTarget:self withObject:self.view];


    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
      // Data to populate the tableview
      [[NSOperationQueue mainQueue] addOperationWithBlock:^ {

             [self.tableView1 reloadData];
             //Stop the UIActivitiyIndicatorView
             [self stopLoading];

         }];
}];

我能够在数据加载成功时成功解除,但是如果有人在从Web服务加载数据期间触摸屏幕的其余部分,我想解除UIActivityIndicatorView。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

你可以试试吗

显示UIAlertView后,向视图控制器(甚至窗口)添加视图,全新的空UIView全屏尺寸。使用UITapGestureRecognizer

附加到此视图
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[view addGestureRecognizer:singleTap];
[singleTap release];

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [myAlert dismissWithClickedButtonIndex:0 animated:YES];
    [view removeFromSuperView];
}

答案 1 :(得分:0)

您可以按照以下UIResponder方法执行此操作 -(void)touchesBegan-(void)touchesEnded

并禁用其中的UIActivityIndicator

答案 2 :(得分:0)

使用touchesEnded方法

触摸结束方法内的

执行停止和删除UIActivityIndicatorView

的功能
-(void)touchesEnded

{

[indicator stopAnimating];

[indicator removeFromSuperView];

}