首页 技术 正文
技术 2022年11月19日
0 收藏 382 点赞 2,510 浏览 2661 个字

UIView/UIWindow/UIScreen/CALayer

1、UIScreen可以获取设备屏幕的大小。

1234567 // 整个屏幕的大小 {{0, 0}, {320, 480}}CGRect bounds = [UIScreen mainScreen].bounds;NSLog(@"UIScreen bounds: %@", NSStringFromCGRect(bounds)); // 应用程序窗口大小 {{0, 20}, {320, 460}}CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;NSLog(@"UIScreen applicationFrame: %@", NSStringFromCGRect(applicationFrame));

2、UIView对象定义了一个屏幕上的一个矩形区域,同时处理该区域的绘制和触屏事件。
可以在这个区域内绘制图形和文字,还可以接收用户的操作。一个UIView的实例可以包含和管理若干个子UIView。

ViewController.m

1234567 -
(
void)viewDidAppear:(BOOL)animated{    [super
viewDidAppear:animated];
    UIView*
myView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    myView.backgroundColor=[UIColor
redColor];
    [self.view
addSubview:myView];
}

3、UIWindow对象是所有UIView的根,管理和协调的应用程序的显示
UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。

AppDelegate.m

12345678 -
(
BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    UIWindow
*myWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    myWindow.backgroundColor=[UIColor
whiteColor];
    [myWindow
makeKeyAndVisible];
    _window
= myWindow;
    return

YES;
}

4、UIViewController对象负责管理所有UIView的层次结构,并响应设备的方向变化。

AppDelegate.m

12345678910 -
(
BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    UIWindow
*myWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    myWindow.backgroundColor=[UIColor
whiteColor];
    UIViewController
*myViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    myWindow.rootViewController
= myViewController;
    [myWindow
makeKeyAndVisible];
    _window
= myWindow;
    return

YES;
}

  CALayer是在/System/Library/Frameworks/QuartzCore.framework定义的。而且CALayer作为一个低级的,可以承载绘制内容的底层对象出现在该框架中。

现在比较一下uiview和calayer都可以显示图片文字等信息。难道apple提供了,两套绘图机制吗?不会。

UIView相比CALayer最大区别是UIView可以响应用户事件,而CALayer不可以。UIView侧重于对显示内容的管理,CALayer侧重于对内容的绘制。

大家都知道QuartzCore是IOS中提供图像绘制的基础库。并且CALayer是定义该框架中。难道UIView的底层实现是CALayer??

官方做出了明确的解释:

Displaying Layers in Views

Core Animation doesn’t provide a means for actually displaying layers in a window, they must be hosted by a view. When paired with a view, the view must provide event-handling for the underlying layers, while the layers provide display of the content.

The view system in iOS is built directly on top of Core Animation layers. Every instance of UIView automatically creates an instance of a CALayer class and sets it as the value of the view’s layer property. You can add sublayers to the view’s layer as needed.

On Mac OS X you must configure an NSView instance in such a way that it can host a layer.

由此可见UIView是基于CALayer的高层封装。The view system in iOS is built directly on top of Core Animation layers.

结论:

UIView来自CALayer,高于CALayer,是CALayer高层实现与封装。UIView的所有特性来源于CALayer支持。

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901