ios UIView 和 CALayer 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7826306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between a UIView and a CALayer?
提问by Oleg Danu
Both have most of the same attributes, both support different kind of animations, both represent different data. What are the differences between a UIView and a CALayer?
两者具有大部分相同的属性,都支持不同类型的动画,都代表不同的数据。UIView 和 CALayer 之间有什么区别?
回答by Brad Larson
On iOS, every UIView is backed by a Core Animation CALayer, so you are dealing with CALayers when using a UIView, even though you may not realize it. Unlike NSViews on the Mac, which evolved before Core Animation existed, UIViews are intended to be lightweight wrappers around these CALayers.
在 iOS 上,每个 UIView 都由 Core Animation CALayer 支持,因此您在使用 UIView 时正在处理 CALayer,即使您可能没有意识到。与 Mac 上的 NSViews 在 Core Animation 存在之前进化不同,UIViews 旨在成为这些 CALayer 的轻量级包装器。
As I describe in the similar question "When to use CALayer on the Mac/iPhone?", working directly with CALayers doesn't give you significant performance advantages over UIViews. One of the reasons you might want to build a user interface element with CALayers instead of UIViews is that it can be very easily ported to the Mac. UIViews are very different from NSViews, but CALayers are almost identical on the two platforms. This is why the Core Plot framework lays out its graphs using CALayers instead of other UI elements.
正如我在类似问题“何时在 Mac/iPhone 上使用 CALayer?”中所描述的那样。,与 UIViews 相比,直接使用 CALayer 不会给您带来显着的性能优势。您可能想要使用 CALayer 而不是 UIViews 构建用户界面元素的原因之一是它可以非常容易地移植到 Mac。UIViews 与 NSViews 非常不同,但 CALayer 在两个平台上几乎相同。这就是 Core Plot 框架使用 CALayer 而不是其他 UI 元素来布置其图形的原因。
One thing UIViews provide over CALayers is built-in support for user interaction. They handle hit-testing on touches and other related actions that you would need to build yourself if managing a hierarchy of CALayers. It's not that hard to implement this yourself, but it is extra code you'd need to write when building a CALayer-only interface.
UIViews 通过 CALayer 提供的一件事是对用户交互的内置支持。如果管理 CALayer 的层次结构,它们会处理您需要自己构建的触摸和其他相关操作的命中测试。自己实现它并不难,但在构建仅 CALayer 的接口时,您需要编写额外的代码。
You will often need to access the underlying layers for a UIView when performing more complex animations than the base UIView class allows. UIView's animation capabilities have grown as the iOS SDK has matured, but there are still a few things that are best done by interacting with the underlying CALayer.
当执行比基 UIView 类允许的更复杂的动画时,您通常需要访问 UIView 的底层层。UIView 的动画功能随着 iOS SDK 的成熟而增长,但仍有一些事情最好通过与底层 CALayer 交互来完成。
回答by Mat
From the Ray Wenderlich blog (Tutorial)
来自 Ray Wenderlich 博客(教程)
CALayers are simply classes representing a rectangle on the screen with visual content. “But wait a darn minute,” you may say, “that's what UIViews are for!” That's true, but there's a trick to that: every UIView contains a root layer that it draws to!
CALayer 只是表示屏幕上具有视觉内容的矩形的类。“但是等一下,”你可能会说,“这就是 UIViews 的用途!” 这是真的,但有一个技巧:每个 UIView 都包含一个它绘制的根层!
回答by Forrest
Simply speaking,UIView inherit from UIResponder, handle events from users, contains CALayer, which inherit from NSObject, mainly focus on rendering, animation etc.
简单来说,UIView继承自UIResponder,处理来自用户的事件,包含CALayer,继承自NSObject,主要关注渲染,动画等。
回答by Gobi M
UIView
is a container for CALayers
. Using UIKit
.
UIView
是 的容器CALayers
。使用UIKit
.
CALayer
where we draw the contents. Using CoreGraphics
CALayer
我们绘制内容的地方。使用CoreGraphics
If you work with custom control like features it would be great to go ahead with single view containing more layers for accurate native rendering. Since CALayers
are weightless than UIView
.
如果您使用自定义控件之类的功能,那么继续使用包含更多层的单个视图进行准确的本地渲染会很棒。由于CALayers
比 失重UIView
。
To create common skeleton for Mac and iOS, follow the design for your app using CALayers
. Since it is available in both platform.
要为 Mac 和 iOS 创建通用框架,请使用CALayers
. 因为它在两个平台上都可用。
UIView
having feature like touch events achieved using delegates -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
, tochesStart
like events and other UIKit
features.
UIView
具有使用委托实现的触摸事件等功能-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
,tochesStart
例如事件和其他UIKit
功能。
To work with CALayers
use Core Graphics knowledge.For any simple view rendering UIView
is enough.
CALayers
使用 Core Graphics 知识来工作。对于任何简单的视图渲染UIView
就足够了。
回答by Anit Kumar
UIView:Views have more complex hierarchy layouts. They can receive user interactions like taps, pinches, cliks and more. Working with UIViews happens on the main thread, it means it is using CPU power.
UIView:视图具有更复杂的层次结构布局。他们可以接收用户交互,如点击、捏合、点击等。使用 UIViews 发生在主线程上,这意味着它正在使用 CPU 能力。
CALayer:Layers on other hand have simpler hierarchy. That means they are faster to resolve and quicker to draw on the screen. There is no responder chain overhead unlike with views. Layers are drawn directly on the GPU. It happens on a separate thread without burdening the CPU.
CALayer:另一方面,图层具有更简单的层次结构。这意味着它们可以更快地解决并更快地在屏幕上绘制。与视图不同,没有响应链开销。层是直接在 GPU 上绘制的。它发生在一个单独的线程上,不会给 CPU 带来负担。
For more details: https://medium.com/@fassko/uiview-vs-calayer-b55d932ff1f5
更多详情:https: //medium.com/@fassko/uiview-vs-calayer-b55d932ff1f5
回答by musixlemon
The big difference is UIView is designed for CocoaTouch on mobile device. It adds some event handler which CALayer did not provide.
最大的不同是 UIView 是为移动设备上的 CocoaTouch 设计的。它添加了一些 CALayer 没有提供的事件处理程序。