Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable all Input while UIActivityIndicatorView spinning

How can I disable all Input while the UIActivityIndicatorView is spinning?

Thanks

like image 521
mica Avatar asked Mar 04 '12 11:03

mica


3 Answers

You can call beginIgnoringInteractionEvents when you start the spinner

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

and endIgnoringInteractionEvents when you stop it.

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

Just make sure your code always comes to the point where you call endIgnoringInteractionEvents, otherwise your app will freeze (from the users point of view).

like image 79
Rok Jarc Avatar answered Oct 14 '22 12:10

Rok Jarc


In Swift 3.0:

To disable interaction:

UIApplication.shared.beginIgnoringInteractionEvents() 

To restore interaction:

UIApplication.shared.endIgnoringInteractionEvents() 
like image 34
Shailendra Suriyal Avatar answered Oct 14 '22 10:10

Shailendra Suriyal


Just an addition to rokjarc answer. Here an example of watchdog to keep app alive. You can call always with some critical interval, maybe 10 sec. And if you need to enable within 10 sec, just call "enable" method.

UIWindow * __weak mainWindow;

- (void)disableGlobalUserInteractionForTimeInterval:(NSTimeInterval)interval
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mainWindow = [[[UIApplication sharedApplication] windows] lastObject];
    });

    [mainWindow setUserInteractionEnabled:false];

    if (interval > 0)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self enableGlobalUserInteraction];
        });
    }
}

- (void)enableGlobalUserInteraction
{
    if (mainWindow)
    {
        [mainWindow setUserInteractionEnabled:true];
    }
}
like image 1
Mike Glukhov Avatar answered Oct 14 '22 10:10

Mike Glukhov