模仿iOS上的截图flash动画

时间:2013-09-29 16:25:53

标签: ios

我正在寻找一种复制“闪烁”动画的方法,即在按住home + lock时播放。

有人知道这个动画是否以某种方式可用?

5 个答案:

答案 0 :(得分:10)

在iOS设备上,当您按住home + lock并屏幕闪烁白色时,您会截取屏幕截图。你的意思是这个效果吗?如果是这样,试试这个:

将具有白色背景颜色的UIView添加到视图层次结构中,使其覆盖整个屏幕。然后,启动一个动画,将此视图的不透明度淡化为零。完成后,从超级视图中删除视图:

[UIView animateWithDuration: 0.5 
                 animations: ^{
                               whiteView.alpha = 0.0;
                             }
                 completion: ^(BOOL finished) {
                               [whiteView removeFromSuperview];
                             }
];

答案 1 :(得分:3)

尝试:

[UIView animateWithDuration:1 animations:^{
    self.view.backgroundColor = [UIColor blackColor];
    for (UIView *view2 in self.view.subviews) {
        view2.backgroundColor = [UIColor blackColor];
    }
}];
[UIView animateWithDuration:1 animations:^{
    self.view.backgroundColor = [UIColor whiteColor];
    for (UIView *view2 in self.view.subviews) {
        view2.backgroundColor = [UIColor whiteColor];
    }
}];

答案 2 :(得分:2)

我发现很容易模仿Apple Flash使用的截图。我希望大家至少尝试过一次。

UIView * flashView = [[UIView alloc] initWithFrame:self.view.frame];
flashView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:flashView];

[UIView animateWithDuration:1 delay:0.3 options:0 animations:^{
      flashView.alpha = 0;
    } completion:^(BOOL finished)
     {
        [flashView removeFromSuperview]; 
     }];
}

答案 3 :(得分:1)

这些例子给了我灵感,但没有给我预期的效果。这是我的尝试,看起来非常接近真实的交易。

func mimicScreenShotFlash() {

    let aView = UIView(frame: self.view.frame)
    aView.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(aView)

    UIView.animateWithDuration(1.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        aView.alpha = 0.0
        }, completion: { (done) -> Void in
            aView.removeFromSuperview()
    })
}

答案 4 :(得分:0)

当前解决方案存在两个问题:

  1. 将视图放在其他视图上(根据我使用UICollectionView和其他废话的经验)可以触发自动布局。 Autolayout很糟糕。 Autolayout使机器完成一系列工作,并且可以产生副作用,而不仅仅是计算除了刻录CPU和电池之外的其他原因。所以,你不想给它自动布局的理由,例如,将一个子视图添加到一个真正非常关心保持自己布局的视图。

  2. 你的视图并不一定覆盖整个屏幕,所以如果你想要整个屏幕,你最好使用UIWindow ...这应该与任何看起来很敏感的视图控制器绝缘子视图已添加

  3. 这是我的实现,UIView上的一个类别。我已经包含了在闪烁窗口之前截取屏幕截图的方法,然后将其保存到相机胶卷中。请注意,UIWindow似乎想要在添加时自己动画,它通常会在三分之一秒内淡入淡出。可能有更好的方法告诉它不要这样做。

    // stupid blocks
    typedef void (^QCompletion)(BOOL complete);
    
    @interface UIView (QViewAnimation)
    + (UIImage *)screenshot; // whole screen 
    + (void)flashScreen:(QCompletion)complete;
    - (UIImage *)snapshot; // this view only
    - (void)takeScreenshotAndFlashScreen;
    @end
    
    @implementation UIView (QViewAnimation)
    
    + (UIImage *)screenshot;
    {
    
        NSArray *windows = [[UIApplication sharedApplication] windows];
        UIWindow *window = nil;
        if (windows.count) {
            window = windows[0];
            return [window snapshot];
        } else {
            NSLog(@"Screenshot failed.");
            return nil;
        }
    }
    
    - (UIImage *)snapshot;
    {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0);
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    + (void)flashScreen:(QCompletion)complete;
    {
        UIScreen *screen = [UIScreen mainScreen];
        CGRect bounds = screen.bounds;
        UIWindow * flash = [[UIWindow alloc] initWithFrame:bounds];
        flash.alpha = 1;
        flash.backgroundColor = [UIColor whiteColor];
    
        [UIView setAnimationsEnabled:NO];
        [flash makeKeyAndVisible];
        [UIView setAnimationsEnabled:YES];
    
        [UIView animateWithDuration:0.3
                              delay:0
                            options:UIViewAnimationCurveEaseOut
                         animations:^{
                flash.alpha = 0;
            }
                             completion: ^(BOOL finished)
            {
                flash.hidden = YES;
                [flash autorelease];
                if (complete) {
                    complete(YES);
                }
    
            }];
    }
    
    - (void)takeScreenshotAndFlashScreen;
    {
        UIImage *image = [UIView screenshot];
        [UIView flashScreen:^(BOOL complete){
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),
                           ^{
                UIImageWriteToSavedPhotosAlbum(image,self,@selector(imageDidFinishSaving:withError:context:),nil);
            });
        }];
    }
    
    - (void)imageDidFinishSaving:(UIImage *)image
                       withError:(NSError *)error
                         context:(void *)context;
    {
        dispatch_async(dispatch_get_main_queue(),^{
            // send an alert that the image saved
        });
    }
    
    @end