如何以编程方式闪屏?

时间:2011-03-10 01:45:16

标签: iphone cocoa xcode

经过长时间的搜索,我不得不放弃并问。

是否可以闪屏(就像使用主页按钮+电源按钮截屏一样)?

如果是,那怎么样?

提前感谢您的回答。

2 个答案:

答案 0 :(得分:6)

将白色全屏UIView添加到窗口并为其动画显示为alpha(使用持续时间和动画曲线播放以获得所需的结果):

 -(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 :(得分:0)

类似于Max提供的答案,但使用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];
                 }];
}

有animateWithDuration的不同版本,例如,如果您不需要延迟并且可以使用默认动画选项,则可以使用此较短版本。

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