how to display UIActivityIndicatorView BEFORE rota

2019-01-20 20:03发布

问题:

I'd like to display an activity indicator BEFORE the work undertaken by willAnimateRotationToInterfaceOrientation:duration: begins. Most of the time in my app, this work is quickly completed and there would be no need for an activity indicator, but occasionally (first rotation, i.e. before I have cached data, when working with a large file) there can be a noticeable delay. Rather than re-architect my app to cope with this uncommon case, I'd rather just show the UIActivityIndicatorView while the app generates a cache and updates the display.

The problem is (or seems to be) that the display is not updated between the willRotateToInterfaceOrientation:duration and the willAnimateRotationToInterfaceOrientation:duration: method. So asking iOS to show UIActivityIndicator view in willRotate method doesn't actually affect the display until after the willAnimateRotation method.

The following code illustrates the issue. When run, the activity indicator appears only very briefly and AFTER the simulateHardWorkNeededToGetDisplayInShapeBeforeRotation method has completed.

Am I missing something obvious? And if not, any smart ideas as to how I could work around this issue?

Update: While suggestions about farming the heavy lifting off to another thread etc. are generally helpful, in my particular case I kind of do want to block the main thread to do my lifting. In the app, I have a tableView all of whose heights need to be recalculated. When - which is not a very common use case or I wouldn't even be considering this approach - there are very many rows, all the new heights are calculated (and then cached) during a [tableView reloadData]. If I farm the lifting off and let the rotate proceed, then after the rotate and before the lifting, my tableView hasn't been re-loaded. In the portrait to landscape case, for example, it doesn't occupy the full width. Of course, there are other workarounds, e.g. building a tableView with just a few rows prior to the rotate and then reloading the real one over that etc.

Example code to illustrate the issue:

@implementation ActivityIndicatorViewController

@synthesize activityIndicatorView = _pgActivityIndicatorView;
@synthesize label = _pgLabel;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    NSLog(@"willRotate");

    [self showActivityIndicatorView];
}


- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    NSLog(@"willAnimateRotation");
    [self simulateHardWorkNeededToGetDisplayInShapeBeforeRotation];
}


- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
{
    NSLog(@"didRotate");
    [self hideActivityIndicatorView];
}


- (void) simulateHardWorkNeededToGetDisplayInShapeBeforeRotation;
{
    NSLog(@"Starting simulated work");
    NSDate* date = [NSDate date];

    while (fabs([date timeIntervalSinceNow]) < 2.0)
    {
        //
    }
    NSLog(@"Finished simulated work");
}


- (void) showActivityIndicatorView;
{
    NSLog(@"showActivity");
    if (![self activityIndicatorView])
    {
        UIActivityIndicatorView* activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self setActivityIndicatorView:activityIndicatorView];
        [[self activityIndicatorView] setCenter:[[self view] center]];
        [[self activityIndicatorView] startAnimating];
        [[self view] addSubview: [self activityIndicatorView]];
    }

    // in shipping code, an animation with delay would be used to ensure no indicator would show in the good cases
    [[self activityIndicatorView] setHidden:NO];
}

- (void) hideActivityIndicatorView;
{
    NSLog(@"hideActivity");
    [[self activityIndicatorView] setHidden:YES];
}


- (void) dealloc;
{
    [_pgActivityIndicatorView release];
    [super dealloc];
}


- (void) viewDidLoad;
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 50.0, 0.0, 0.0)];
    [label setText:@"Activity Indicator and Rotate"];
    [label setTextAlignment: UITextAlignmentCenter];
    [label sizeToFit];
    [[self view] addSubview:label];
    [self setLabel:label];
    [label release];
}

@end

回答1:

The app doesn't update the screen to show the UIActivityIndicatorView until the main run loop regains control. When a rotation event happens, the willRotate... and willAnimateRotation... methods are called in one pass through the main run loop. So you block on the hard work method before displaying the activity indicator.

To make this work, you need to push the hard work over to another thread. I would put the call to the hard work method in the willRotate... method. That method would call back to this view controller when the work is completed so the view can be updated. I would put show the activity indicator in the willAnimateRotation... method. I wouldn't bother with a didRotateFrom... method. I recommend reading the Threaded Programming Guide.

Edit in response to a comment: You can effectively block user interaction by having the willAnimateRotation... method put a non functioning interface on screen such as a view displaying a dark overlay over and the UIActivityIndicatorView. Then when the heavy lifting is done, this overlay is removed, and the interface becomes active again. Then the drawing code will have the opportunity to properly add and animate the activity indicator.



回答2:

More digging (first in Matt Neuberg's Programming iPhone 4) and then this helpful question on forcing Core Animation to run its thread from stackoverflow and I have a solution that seems to be working well. Both Neuberg and Apple issue strong caution about this approach because of the potential for unwelcome side effects. In testing so far, it seems to be OK for my particular case.

Changing the code above as follows implements the change. The key addition is [CATransaction flush], forcing the UIActivityIndicatorView to start displaying even though the run loop won't be ended until after the willAnimateRotationToInterfaceOrientation:duration method completes.

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    NSLog(@"willRotate");

    [self showActivityIndicatorView];
    [CATransaction flush];  // this starts the animation right away, w/o waiting for end of the run loop
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    NSLog(@"willAnimateRotation");
    [self simulateHardWorkNeededToGetDisplayInShapeBeforeRotation];
    [self hideActivityIndicatorView];
}


- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
{
    NSLog(@"didRotate");
}


回答3:

Try performing you work on a second thread after showing the activity view.

[self showActivityIndicatorView];
[self performSelector:@selector(simulateHardWorkNeededToGetDisplayInShapeBeforeRotation) withObject:nil afterDelay:0.01];


回答4:

Either execute the heavy lifting in a background thread and post the results in the foreground thread to update the UI (UIKit is only thread safe since iOS 4.0):

[self performSelectorInBackground:@selector(simulateHardWorkNeededToGetDisplayInShapeBeforeRotation) withObject:nil]

Or you can schedule the heavy lifting method to be executed after the rotation took place:

[self performSelector:@selector(simulateHardWorkNeededToGetDisplayInShapeBeforeRotation) withObject:nil afterDelay:0.4]

But these are only hacks and the real solution is to have proper background processing if your UI needs heavy processing to get updated, may it be in portrait or landscape. NSOperation and NSOperationQueue is a good place to start.