我正在寻找一种复制“闪烁”动画的方法,即在按下 home+lock 时播放。
有谁知道这个动画是否以某种方式可用?
在 iOS 设备上,当您按下 home + lock 并且屏幕闪烁白色时,您会截取屏幕截图。你是说这个效果吗?如果是这样,试试这个:
将UIView
具有白色背景颜色的 a 添加到您的视图层次结构中,使其覆盖整个屏幕。然后,启动一个动画,将该视图的不透明度淡化为零。完成后,从其父视图中删除视图:
[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];
}
}];
这些例子给了我灵感,但并没有给我想要的效果。这是我的尝试,看起来非常接近真实交易。
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()
})
}
我找到了一种非常简单的方法来完全模仿 Apple 使用的 Screenshot 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];
}];
}
当前解决方案的两个问题:
将视图放在其他视图上可以(根据我对 UICollectionView 和其他废话的经验)触发自动布局。自动布局很糟糕。Autolayout 使机器做了大量的工作,除了烧 CPU 和电池之外,除了无缘无故地计算之外,还会产生副作用。所以,你不想给它自动布局的理由,例如,将一个子视图添加到一个真正非常关心保持自身布局良好的视图中。
你的视图不一定覆盖整个屏幕,所以如果你想闪烁整个屏幕,你最好使用 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