2011-03-10 4 views
4

Après une longue période de recherche, je dois abandonner et demander.Comment faire pour flasher l'écran par programmation?

Est-il possible de écran flash (tout comme dans la prise de capture d'écran en utilisant le bouton d'accueil + bouton d'alimentation)?

Si oui, alors comment?

Merci d'avance pour les réponses.

Répondre

6

Ajouter le blanc UIView plein écran à la fenêtre et l'animer est l'alpha (jouer avec la courbe durée et animation pour obtenir le résultat que vous voulez):

-(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: ne pas oublier de retirer ce point de vue après l'animation est terminée

+1

N'oubliez pas de tout sortir lorsque vous avez terminé :) –

+0

C'est une réponse étonnamment rapide et correcte. Merci beaucoup Max !! – Patryk

0

similaires à la réponse fournie par Max, mais en utilisant UIView animateWithDuration à la place

- (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]; 
       }]; 
} 

Il existe différentes versions de animateW ithDuration, vous pouvez par exemple également utiliser cette version plus courte, si vous n'avez pas besoin d'un délai et que vous êtes d'accord avec les options d'animation par défaut.

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