首页 > 代码库 > [翻译] snapshotViewAfterScreenUpdates
[翻译] snapshotViewAfterScreenUpdates
snapshotViewAfterScreenUpdates
This method very efficiently captures the current rendered appearance of a view and uses it to build a new snapshot view. You can use the returned view as a visual stand-in for the current view in your app. For example, you might use a snapshot view for animations where updating a large view hierarchy might be expensive. Because the content is captured from the already rendered content, this method reflects the current visual appearance of the view and is not updated to reflect animations that are scheduled or in progress. However, calling this method is faster than trying to render the contents of the current view into a bitmap image yourself.
这个方法能够高效的将当前显示的view截取成一个新的view.你可以用这个截取的view用来显示.例如,也许你只想用一张截图来做动画,毕竟用原始的view做动画代价太高.因为是截取了已经存在的内容,这个方法只能反应出这个被截取的view当前的状态信息,而不能反应这个被截取的view以后要显示的信息.然而,不管怎么样,调用这个方法都会比将view做成截图来加载效率更高.
Because the returned snapshot is a view object, you can modify it and its layer object as needed. However, you cannot change the contents property of the snapshot view’s layer; attempts to do so fail silently. If the current view is not yet rendered, perhaps because it is not yet onscreen, the snapshot view has no visible content.
因为返回的是一个view对象,所以,你可以更改它以及它的layer属性.但是呢,你不能够修改它的layer的content属性;如果你试图这么做,将不会有任何效果.如果当前的view还没有渲染,或者这么说吧,因为还没有出现在屏幕上,那么,这个截取的view将不会有能显示的content.
You can call this method on a previously generated snapshot to obtain a new snapshot. For example, you could do so after you change properties of a previous snapshot (such as its alpha value) and want a new snapshot that includes those changes.
你也可以用这个方法在截取的view的基础上截取出一张新的view出来.
If you want to apply a graphical effect, such as blur, to a snapshot, use the drawViewHierarchyInRect:afterScreenUpdates: method instead.
如果你想要加载一个图形效果,比如blur,请使用这个方法drawViewHierarchyInRect:afterScreenUpdates:来代替.
以下是运行效果(左上角是被截取的view,中间是截取的view):
源码:
//// RootViewController.m// SnapshotView//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "YXGCD.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; // 原始的view UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; showView.backgroundColor = [UIColor redColor]; [self.view addSubview:showView]; // 截取的view UIView *snap1 = [showView snapshotViewAfterScreenUpdates:YES]; snap1.center = self.view.center; [self.view addSubview:snap1]; // 7s钟之后给原始的view进行颜色的变换 [[GCDQueue mainQueue] execute:^{ [UIView animateWithDuration:5 animations:^{ showView.backgroundColor = [UIColor yellowColor]; }]; } afterDelay:NSEC_PER_SEC * 7];}@end
这个方法是iOS7中新出的,以后再也不用把view渲染成截图来加载了:).