What are the differences between a UIView and a CALayer?(UIView 和 CALayer 有什么区别?)
问题描述
两者具有大部分相同的属性,都支持不同类型的动画,都表示不同的数据.UIView 和 CALayer 有什么区别?
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?
推荐答案
在 iOS 上,每个 UIView 都由 Core Animation CALayer 支持,因此在使用 UIView 时您正在处理 CALayers,即使您可能没有意识到这一点.与 Mac 上的 NSView 不同,它在 Core Animation 存在之前就已经发展起来,UIView 旨在成为这些 CALayer 周围的轻量级包装器.
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.
正如我在类似问题中描述的那样 "何时使用 CALayerMac/iPhone?",直接使用 CALayers 并没有给你带来比 UIViews 显着的性能优势.您可能想要使用 CALayers 而不是 UIViews 构建用户界面元素的原因之一是它可以很容易地移植到 Mac.UIViews 与 NSViews 非常不同,但 CALayers 在两个平台上几乎相同.这就是 Core Plot 框架使用 CALayers 而不是其他 UI 元素来布置其图表的原因.
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.
UIViews 通过 CALayers 提供的一件事是对用户交互的内置支持.如果管理 CALayers 的层次结构,它们会处理您需要自己构建的触摸和其他相关操作的命中测试.自己实现这一点并不难,但是在构建仅 CALayer 的接口时需要编写额外的代码.
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.
在执行比基本 UIView 类允许的更复杂的动画时,您通常需要访问 UIView 的底层.UIView 的动画功能随着 iOS SDK 的成熟而增长,但仍有一些事情最好通过与底层 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 和 CALayer 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIView 和 CALayer 有什么区别?
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01