Display UIActivityIndicator value in x seconds

I have a UITableViewController which is a RootViewController. It has only a tabular view. I add the UIActivityIndicator identifier through IB and create an IBOutlet. I programmatically added a toolbar with a delete button to the RootViewController. When the user clicks the delete button on the toolbar, I want to display an indicator in the center of the screen. The method below deletes data from the database, which happens very quickly. I want the user to know that something is happening. If I run the code below without sleep (), this happens quickly and I do not see the indicator. But some, for some reason, I do not see the indicator in any case. I assume sleep () blocks repainting? If I survive the dream () and the last two lines, I see the indicator, but, of course, it never disappears. It is also displayed in the upper left corner of the window.

How can I get the code below to work so that the indicator displays for at least 1/2 second?

How to align the indicator in the middle of the window?

[self.navigationController.view addSubview:activityIndicator];
[activityIndicator startAnimating];
[activityIndicator setNeedsDisplay];

//do something which might happen really fast

sleep(1); //create illusion
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
+3
source share
4 answers

The counter will not start spinning until it processes the event and returns to the execution loop. The way around this is to ask the execution loop to complete everything for you.

Here is an example of what I mean:

- (IBAction)deleteAction:(id)sender {
    [self.navigationController.view addSubview:activityIndicator];
    [activityIndicator startAnimating];

    // Spinner won't start spinning until we finish processing this event, so
    // we're just going to schedule the rest of what we need to do.

    // doDelete: will run when the main thread gets its next event.
    [self performSelectorOnMainThread:@selector(doDelete:)
                           withObject:record
                        waitUntilDone:NO];

    // removeSpinner: will run in at least one second, but will wait if
    // another event (like the doDelete: one) is in the middle of running.
    [self performSelector:@selector(removeSpinner:)
               withObject:activityIndicator
               afterDelay:1.0];
}
- (void)doDelete:(id)record {
    [record delete];   // or whatever it is you need to do
}
- (void)removeSpinner:(UIActivityIndicator*)activityIndicator {
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
}

Note: there is nothing in this code that would guarantee to stop it from processing other touches during sleep, so make sure that you somehow block other events.

+6
source

, , ( ) . , Brent. , , , , , .

- (void)removeSpinner:(id)record {
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank You" message:@"Thanks for your feedback!"
                                                                                                 delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    // return to feedings tab
  tabBarController.selectedIndex = 0;
}


- (void)doFeedback:(id)record {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    int j;
    for (int i = 0; i < 1000000000; i++) {
        j = j + i;
    }
    [self performSelectorOnMainThread:@selector(removeSpinner:) withObject:nil waitUntilDone:NO];

    [pool release];
}


- (IBAction)handleSend {        
    [self.view addSubview:activityIndicator];
    [activityIndicator startAnimating];

    // Spinner won't start spinning until we finish processing this event, so
    // we're just going to schedule the rest of what we need to do. 
    // doFeedback: will run when the main thread gets its next event.   
    [self performSelectorInBackground:@selector(doFeedback:) withObject:nil];
}
+1

, , , . , , . , .

0

[self.navigationController.view addSubview: activityIndicator]; [activityIndicator startAnimating];

[self performSelector: @selector (removeSpinner :) withObject: activityIndicator afterDelay: 5.0];

0
source

Source: https://habr.com/ru/post/1704109/


All Articles