[翻译] 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渲染成截图来加载了:).

目录
相关文章
|
存储 自然语言处理 前端开发
从零写一个Recoil(翻译)
Rewriting Recoil from scratchrecoil是facebook编写的一个库,它之所以诞生是因为人体工程学、context的性能问题和useState。这是一个非常聪明的库,几乎每个人都会找到它的用途——如果你想了解更多,请查看这段解释视频。刚开始我被图论和recoil惊到了,但渐渐的理解后,感觉也没那么特别了。也许我也可以实现一个类似的东西。我自己实现的版本和recoil
264 0
从零写一个Recoil(翻译)
|
XML Java Android开发
[翻译]IAdaptable是什么?
IAdaptable在Eclipse里是一个非常重要的接口。对于Eclipse开发老手来说,它就像异常处理和抽象类一样寻常;但是对新手而言,它却令人感到困惑和畏惧。这篇文章将向你解释IAdaptable到底是什么,以及它在Eclipse里起到的作用。
1226 0
《Wir wilden weisen Frauen》翻译——<连载>
上周去逛图书馆,借了本德语原版的书。今天突然想起来了,就翻开来看。我不知道这本书是否有中文版的,不管有没有吧,我尝试翻译一下以提高自身的德语水平。每天花上一个半小时来翻译,我想应该不能翻译出来几句的。
811 0
|
缓存 API iOS开发