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

查看:260
本文介绍了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支持动画CALayer,所以你在使用UIView时正在处理CALayers,即使你可能没有意识到。与核心动画存在之前发展的Mac上的NSView不同,UIViews旨在成为围绕这些CALayers的轻量级包装器。

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.

正如我在类似问题中描述的那样何时在Mac / iPhone上使用CALayer?,直接与与UIViews相比,CALayers不会给您带来显着的性能优势。您可能希望使用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的底层。随着iOS SDK的成熟,UIView的动画功能不断增强,但仍然有一些事情最好通过与底层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天全站免登陆