问题
I’m building an article reading app. I’m using AMSliderMenu(https://github.com/SocialObjects-Software/AMSlideMenu) library for menu list.
I am unable to implement UIActivityIndicator
, when i click on table cell in AMSlideMenu it takes time to load another view because data is load into anther view.
I want to give UIActivityIndicator
when user click on cell UIActivityIndicator
perform till the time it opens another view.
Here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
spinner.hidesWhenStopped = YES;
[self.view addSubview:spinner];
[spinner startAnimating];
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^{
[NSThread sleepForTimeInterval:10];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
});
});
NSString *segueIdentifier = [self.mainVC segueIdentifierForIndexPathInLeftMenu:indexPath];
if (segueIdentifier && segueIdentifier.length > 0)
{
[self performSegueWithIdentifier:segueIdentifier sender:self];
}
}
回答1:
Declare the UIActivityIndicatorView
in the class level scope.
UIActivityIndicatorView *spinner;
Perform the segue in the main queue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
spinner.hidesWhenStopped = YES;
[self.view addSubview:spinner];
[spinner startAnimating];
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^{
[NSThread sleepForTimeInterval:10];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *segueIdentifier = [self.mainVC segueIdentifierForIndexPathInLeftMenu:indexPath];
if (segueIdentifier && segueIdentifier.length > 0){
[self performSegueWithIdentifier:segueIdentifier sender:self];
}else{
[spinner stopAnimating];
}
});
});
}
Stop the spinner when doing the segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[spinner stopAnimating];
}
来源:https://stackoverflow.com/questions/25666115/how-to-uiactivityindicatorview-during-perform-segue