模仿iOS上的截图flash动画

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

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

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

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

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

尝试:

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

我发现很容易模仿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]; }]; } 

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

 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() }) } 

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

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

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

这是我的实现,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