简体   繁体   English

何时是在UIView子类中设置动画和添加子图层的正确时间

[英]When is the right time to animate and add sublayers in an UIView subclass

If I create a subclassed UIView and want to use a bunch of custom made content (animations via CABasicAnimation using CAShapeLayer s, custom made drawings using CGContextRef in drawRect ) when is the right time to create, add and animate sublayers? 如果我创建一个子类UIView并且想要使用一堆自定义内容(使用CAShapeLayer通过CABasicAnimation制作动画,使用drawRect CGContextRef自定义绘图)何时适合创建,添加子图层并为其设置动画?

I know that I should perform custom drawings in the drawRect method (and since there is the only place when I can actually get UIGraphicsGetCurrentContext() that kinda narrows my choice down). 我知道我应该在drawRect方法中执行自定义绘图(因为只有当我能够实际获得UIGraphicsGetCurrentContext()时才会缩小我的选择范围)。 Now I have been creating sublayers, animations and all that other non-drawing related stuff in the drawRect as well. 现在我一直在drawRect创建子图层,动画和所有其他非绘图相关的东西。 But I'm not sure that drawRect is the best place to do those kind of activities. 但我不确定drawRect是进行这类活动的最佳场所。

I am familiar with layoutSubviews method and some other UIView methods but haven't actually implemented any of them except drawRect . 我熟悉layoutSubviews方法和其他一些UIView方法,但除了drawRect之外,还没有实际实现过任何方法。

So if I repeat myself one more time - the question goes: Where to add sublayers, where to animate them and are there any tricks or catches I should be aware of? 因此,如果我再重复一次 - 问题是:在哪里添加子图层,在哪里设置动画,是否有任何技巧或捕捉我应该注意?

  1. You can create and add the layers at init, if they are permanent. 您可以在init处创建和添加图层(如果它们是永久性的)。 If they are dynamic, layoutSubviews is better. 如果它们是动态的,则layoutSubviews更好。 This obviously means you need to setNeedsLayout whenever a new layer/item is required. 这显然意味着每当需要新的图层/项目时,您需要setNeedsLayout。 You can mix and match as much as you want between -init and -layoutSubviews, but if I had to pick, I'd say lean toward using layoutSubviews. 您可以在-init和-layoutSubviews之间尽可能多地混合和匹配,但如果我必须选择,我会说倾向于使用layoutSubviews。 Don't use drawRect. 不要使用drawRect。

  2. You can set properties (strokeWidth, lineColor, path, etc) to a CAShapeLayer at either the time of creation or during normal execution. 您可以在创建时或正常执行期间将属性(strokeWidth,lineColor,path等)设置为CAShapeLayer。 This includes setting a path to CAShapeLayer. 这包括设置CAShapeLayer的路径。 Again, don't set properties in drawRect. 同样,不要在drawRect中设置属性。

  3. If you want to do custom drawing on the layer you can subclass a layer and use drawRect on that layer. 如果要在图层上进行自定义绘图,可以对图层进行子类化并在该图层上使用drawRect。 This can be good if the CALAyers need to be reused and extended. 如果CALAyers需要重复使用和扩展,这可能会很好。 You can also supply a CALayerDelegate, as long as its not the UIView. 您也可以提供CALayerDelegate,只要它不是UIView。 See these questions: Using CALayer Delegate AND iOS: Using UIView's 'drawRect:' vs. its layer's delagate 'drawLayer:inContext:' 请参阅以下问题: 使用CALayer委托 iOS:使用UIView的'drawRect:'与其图层的delagate'drawLayer:inContext:'

  4. Animation is easy, and it follows the same principles as creating and setting properties. 动画很简单,它遵循与创建和设置属性相同的原则。 Make sure you understand when automatic animations will be invoked and how to disable them: Disabling implicit animations in -[CALayer setNeedsDisplayInRect:] Again don't try to animate from drawRect: How to make my UIBezierPath animated with CAShapeLayer? 确保您了解何时调用自动动画以及如何禁用它们: 在[CALayer setNeedsDisplayInRect:]中禁用隐式动画再次不要尝试从drawRect动画: 如何使用CAShapeLayer动画我的UIBezierPath?

  5. Drawing is expensive. 绘图很贵。 Animation is cheap. 动画很便宜。 Using drawRect too much will cause a big performance hit to your applications. 使用drawRect太多会对您的应用程序造成很大的性能损失。 Use drawRect/setNeedsDisplay sparingly, for instance on a state change (selected/unselected). 谨慎使用drawRect / setNeedsDisplay,例如状态更改(选中/未选中)。 Whenever possible modify views with animations or top level properties, and don't redraw the view. 尽可能使用动画或顶级属性修改视图,并且不要重绘视图。 Making drawRect do anything OTHER than draw could result in you calling setNeedsDisplay unnecessarily. 使drawRect做除绘制之外的任何事情都可能导致您不必要地调用setNeedsDisplay。 An easy upfront optimization is to call drawRect as little as possible, or not at all. 一个简单的前期优化是尽可能少地调用drawRect,或者根本不调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM