UIView 和 CALayer 有什么区别? [英] What are the differences between a UIView and a CALayer?

查看:22
本文介绍了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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆