UIView 和 CALayer的那点事

本文详细探讨了UIView和CALayer的起源、作用及二者之间的关系。UIView作为用户界面的构建者,能响应用户事件;而CALayer则专注于内容的绘制与动画处理。两者相辅相成,UIView依赖CALayer提供内容并进行显示。

UIView 和 CALayer的那点事

(1)老祖

万物归根,UIView和CALayer都是的老祖都是NSObjet。

 

1: UIView的继承结构为: UIResponder : NSObject

 

可以看出UIView的直接父类为UIResponder 类, UIResponder 是gsm的呢?

官方的解释:

The UIResponder class defines an interface for objects that respond to and handle events. It is the superclass of UIApplicationUIView and its subclasses (which include UIWindow). Instances of these classes are sometimes referred to as responder objects or, simply, responders.

 

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content. The UIView class itself provides basic behavior for filling its rectangular area with a background color. More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabelobject draws a text string and a UIImageView object draws an image.

 

可见 UIResponder是用来响应事件的,也就是UIView可以响应用户事件。

 

2:CALayer的继承结构为: NSObject

 

直接从 NSObject继承,因为缺少了UIResponder类,所以CALayer悲催的不能响应任何用户事件。

 

The CALayer class is the model class for layer-tree objects. It encapsulates the position, size, and transform of a layer, which defines its coordinate system. It also encapsulates the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines a layer’s time space.

 

从官方的解释可以看出,CALayer定义了position、size、transform、animations 等基本属性。那UIView的size、frame、position这些属性是从那里来的呢?上面的官方解释没有说明这一点,我们一会再分析

 

至此我们了解到了,UIView 和CALayer的基本信息和主要负责处理的事情。

 

(2)所属框架

1:UIView是在 /System/Library/Frameworks/UIKit.framework中定义的。

这个又是做什么的呢?

The UIKit framework provides the classes needed to construct and manage an application’s user interface for iOS. It provides an application object, event handling, drawing model, windows, views, and controls specifically designed for a touch screen interface.

 

可见UIKit主要是用来构建用户界面,并且是可以响应事件的(得意与UIView的父类UIResponder,至于UIResponderd的实现原理不是这次分析的目的,在此不做过多的解释

 

在这里思考一个问题UIView既然是构建用户界面的,那他是通过什么方式绘制这些图片、文字之类的信息的呢? 

 

Ios中的2D图像绘制都是通过QuartzCore.framework实现的。难道是通过QuartzCore.framework实现的?那又是通过什么方式和QuartzCore.framework联系起来的呢??我们一会再看。

 

2: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 的方法:

layerClass - Implement this method only if you want your view to use a different Core Animation layer for its backing store. For example, if you are using OpenGL ES to do your drawing, you would want to override this method and return the CAEAGLLayer class.

该方法保留了UIView的本质。即对UIView所管理的内容,任何显示也是受到CALayer的影响的。

 

  1. (3)相似支持

1:相似的树形结构

2:显示内容绘制方式

3: 布局约束


  1. (4) UIView 是什么,做什么

UIView是用来显示内容的,可以处理用户事件


  1. (5)CALayer是什么,做什么

CALayer是用来绘制内容的,对内容进行动画处理依赖与UIView来进行显示,不能处理用户事件。


  1. (6)为何有两套结构

并不是两套体系,UIView和CALayer是相互依赖的关系。UIView依赖与calayer提供的内容,CALayer依赖uivew提供的容器来显示绘制的内容。归根到底CALayer是这一切的基础,如果没有CALayer,UIView自身也不会存在,UIView是一个特殊的CALayer实现,添加了响应事件的能力。

 

  1. (7)两者之间的关系

发之于肤,血之于肉,灵之于魄,男人之于肾的关系。依存的关系

 

结论:

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

内容概要:本文档详细介绍了基于直驱永磁同步发电机(PMSG)的1.5MW风力发电系统在Simulink环境下的建模与仿真全过程,涵盖了风力机空气动力学模型、PMSG电磁特性建模、不可控整流与逆变电路、直流环节、空间矢量脉宽调制(SVPWM)技术以及核心控制策略的设计。重实现了最大功率跟踪(MPPT)控制以提升风能捕获效率,并构建了电压外环与电流内环协同工作的双闭环控制系统,通过仿真验证了系统在不同风速条件下稳定运行的能力及动态响应性能。; 适合人群:适用于具备电力系统、电机控制理论基础及Simulink仿真操作经验的研究生、科研人员新能源发电系统开发的工程技术人员;特别适合正在进行风电系统建模、控制算法研究或完成相关毕业设计的专业人士。; 使用场景及目标:①深入理解直驱式PMSG风力发电系统的整体架构与工作机理;②掌握从物理部件建模到控制策略实现的完整Simulink仿真流程;③学习并复现MPPT控制、双闭环控制等关键技术方案;④为后续开展低电压穿越、并网稳定性分析、故障诊断等高级课题提供可靠的仿真平台支撑。; 阅读建议:建议结合Matlab/Simulink软件动手实践,逐模块搭建模型,重关注各控制环节的参数设计与调试方法,同时可参照文中提供的其他风电相关资源进行拓展学习与对比分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值