gpt4 book ai didi

ios - UIView 与 CAShapeLayer 的用法(绘图、动画、用户交互)

转载 作者:行者123 更新时间:2023-11-29 00:10:45 26 4
gpt4 key购买 nike

我有一个非常基本的问题:当你想绘制形状时,你如何在使用 UIViewCAShapeLayer 之间做出选择(我不是在谈论 textfields , switches 或其他控件,只是绘图)?

我的理解是 UIView(作为 UIKit 的一部分)在底层使用普通的 CALayer 来绘制其内容。如果这是正确的,那么 CAShapeLayer(或一般的 CALayer)将是完全相同的东西,只是没有 UIKit 给你的额外功能。

那么,什么时候使用 UIView 才有意义,什么时候使用 CAShapeLayer 才有意义?

CAShapeLayer 更快吗?一般而言,UIKit 是否针对手势识别或用户交互进行了更优化?


为了给您更多背景信息,以下是出现此问题时我正在尝试做的事情:

Illustration

我希望这些红色圆圈围绕中心圆圈旋转。但是,用户应该能够在旋转时点击红色圆圈。

在这里,我看到两个主要选项(可能还有更多)来添加其中一个红色圆圈:

  1. 创建一个 UIView,操纵其层的 cornerRadius 并使用 CGAffineTransform 旋转它。
  2. 使用 UIBezierPath 创建一个 CAShapeLayer。我可以使用 CATransform3D
  3. 旋转它

我在这里遇到的唯一问题是用户交互。由于它不断移动(旋转),我必须访问正确的框架。我可以使用 presentation layer 来做到这一点(我认为 UIView 也在幕后使用)。

此时,我不确定是使用 UIView 还是 CAShapeLayer。另外,我不确定在这种情况下以这种方式设置动画是否是正确的方法。可能有更好的选择,也将消除关于使用哪一个的问题。

感谢您对此的看法。

最佳答案

引自Apple的文档

Layers are not a replacement for your app’s views—that is, you cannot create a visual interface based solely on layer objects. Layers provide infrastructure for your views. Specifically, layers make it easier and more efficient to draw and animate the contents of views and maintain high frame rates while doing so. However, there are many things that layers do not do. Layers do not handle events, draw content, participate in the responder chain, or do many other things. For this reason, every app must still have one or more views to handle those kinds of interactions.

In iOS, every view is backed by a corresponding layer object but in OS X you must decide which views should have layers. In OS X v10.8 and later, it probably makes sense to add layers to all of your views. However, you are not required to do so and can still disable layers in cases where the overhead is unwarranted and unneeded. Layers do increase your app’s memory overhead somewhat but their benefits often outweigh the disadvantage, so it is always best to test the performance of your app before disabling layer support.

When you enable layer support for a view, you create what is referred to as a layer-backed view. In a layer-backed view, the system is responsible for creating the underlying layer object and for keeping that layer in sync with the view. All iOS views are layer-backed and most views in OS X are as well. However, in OS X, you can also create a layer-hosting view, which is a view where you supply the layer object yourself. For a layer-hosting view, AppKit takes a hands off approach with managing the layer and does not modify it in response to view changes.

阅读:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CoreAnimationBasics/CoreAnimationBasics.html#//apple_ref/doc/uid/TP40004514-CH2-SW3

现在进行一些 QNA

问题 1:

My understanding is that UIView (as part of UIKit) uses a normal CALayer under the hood to draw its content. If this is correct, then CAShapeLayer (or CALayer in general) would be the exact same thing, only without the extras UIKit gives you

再次引用苹果文档

Layer-backed views create an instance of the CALayer class by default, and in most cases you might not need a different type of layer object. However, Core Animation provides different layer classes, each of which provides specialized capabilities that you might find useful. Choosing a different layer class might enable you to improve performance or support a specific type of content in a simple way

显然,当您尝试使用 CAShapeLayer 绘制各种形状时,与 CALayer 相比,它在性能方面是有利的。

问题 2

Is CAShapeLayer faster? Is UIKit more optimised for gesture recognition or user interaction in general?

显然,图层不处理事件、绘制内容、参与响应者链或执行许多其他操作。因此,无论是 CALayer 还是 CAShapeLayer,任何层都无法识别用户交互

问题 3

At this point, I'm not sure whether to use UIViews or CAShapeLayers

正如您在问题中指定的那样,您需要红色圆圈的用户交互,并且因为我们现在知道 CAShapeLayer/CALayer 不会响应用户交互这一事实,所以很明显您必须使用 UIView 而不是图层。

关于ios - UIView 与 CAShapeLayer 的用法(绘图、动画、用户交互),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com