2011-03-10 79 views
4

經過很長時間的搜索,我不得不放棄並提問。如何以編程方式閃爍屏幕?

是否有可能閃屏(就像在使用主屏幕按鈕+電源按鈕進行截圖)?

如果是,那麼如何?

在此先感謝您的答案。

回答

6

白色的全屏幕的UIView添加到窗口和動畫它的α(與持續時間和動畫曲線發揮得結果是你想要的):

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

編輯:不要忘了刪除視圖之後動畫結束

+1

也不要忘記釋放的一切,當你做:) – 2011-03-10 02:02:32

+0

這是驚人的快和正確的答案。多謝Max! – Patryk 2011-03-10 02:05:46

0

由馬克斯提供的答案相似,但使用的UIView animateWithDuration代替

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

有不同的版本animateW的ithDuration,如果您不需要延遲並且可以使用默認的動畫選項,那麼您也可以使用這個較短的版本。

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