Patryk
Patryk

Reputation: 85

How to flash screen programmatically?

After long time of searching I have to give up and ask.

Is it possible to flash screen (just like in taking screenshot using home button + power button) ?

If yes, then how ?

Thanks in advance for answers.

Upvotes: 4

Views: 2212

Answers (2)

Simon Bøgh
Simon Bøgh

Reputation: 821

Similar to the answer provided by Max, but using UIView animateWithDuration instead

- (void)flashScreen {
// Make a white view for the flash
UIView *whiteView = [[UIView alloc] initWithFrame:self.view.frame];
whiteView.backgroundColor = [UIColor whiteColor];
whiteView.alpha = 1.0; // Optional, default is 1.0

// Add the view
[self.view addSubview:whiteView];

// Animate the flash
[UIView animateWithDuration:1.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseOut // Seems to give a good effect. Other options exist
                 animations:^{
                     // Animate alpha
                     whiteView.alpha = 0.0;
                 } 
                 completion:^(BOOL finished) {
                     // Remove the view when the animation is done
                     [whiteView removeFromSuperview];
                 }];
}

There are different versions of animateWithDuration, you can for instance also use this shorter version, if you do not need a delay and are ok with the default animation options.

[UIView animateWithDuration:1.0
                 animations:^{
                     // Animate alpha
                     whiteView.alpha = 0.0;
                 } 
                 completion:^(BOOL finished) {
                     // Remove the view when the animation is done
                     [whiteView removeFromSuperview];
                 }];

Upvotes: 0

Max
Max

Reputation: 16719

Add the white full-screen UIView to the window and animate it's alpha (play with duration and animation curve to get result that you want):

 -(void) flashScreen {
    UIWindow* wnd = [UIApplication sharedApplication].keyWindow;
    UIView* v = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, wnd.frame.size.width, wnd.frame.size.height)] autorelease];
    [wnd addSubview: v];
    v.backgroundColor = [UIColor whiteColor];
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationDuration: 1.0];
    v.alpha = 0.0f;
    [UIView commitAnimations];
}

Edit: don't forget to remove that view after animation is ended

Upvotes: 6

Related Questions