简体   繁体   English

iOS自定义UIView绘图 - CAShapeLayers还是drawRect?

[英]iOS Custom UIView Drawing - CAShapeLayers or drawRect?

I'm new to iOS UIView drawing and I'm really trying to implement a custom UIView class in a standard way. 我是iOS UIView绘图的新手,我真的试图以标准方式实现自定义UIView类。

The UIView class that I'm working on now is simple: it has mostly static background shapes that form a composite shape. 我现在正在研究的UIView类很简单:它主要是静态背景形状,形成复合形状。 I also want to add animation to this UIView class so that a simple shape can animate its path on top of the static shapes. 我还想为这个UIView类添加动画,以便一个简单的形状可以在静态形状的顶部设置其路径的动画。

How I'm currently implementing this 我目前是如何实现这一点的

Right now, I'm implementing the static drawing in the drawRect: method of the UIView subclass. 现在,我正在UIView子类的drawRect:方法中实现静态绘图。 This works fine. 这很好用。 I also have not implemented animation of the single shape yet. 我还没有实现单一形状的动画。

What I'm looking to have answered 我想要回答的问题

Question 1: 问题1:

Is it better to have the static shapes drawn in the drawRect: method as I'm currently doing it, or is there a benefit to refactoring all of the shapes being drawn into class-extension-scoped CAShapeLayer properties and doing something like: 是否更好的方法是在drawRect:方法中绘制静态形状,就像我现在正在做的那样,或者重构所有被绘制到类扩展范围的CAShapeLayer属性中的形状并执行如下操作:

-(instancetype) initWithFrame:(CGRect) frame
{
   if (self = [super initWithFrame:frame])
   {
      [self setupView];
   }

   return self;
}

-(void) setupView // Allocate, init, and setup one-time layer properties like fill colorse.
{
   self.shapeLayer1 = [CAShapeLayer layer];
   [self.layer addSubLayer:shapeLayer1];
   self.shapeLayer2 = [CAShapeLayer layer];
   [self.layer addSubLayer:shapeLayer2];
   // ... and so on
}

-(void) layoutSubviews // Set frames and paths of shape layers here.
{
    self.shapeLayer1.frame = self.bounds;
    self.shapeLayer2.frame = self.bounds;

    self.shapeLayer1.path = [UIBezierPath somePath].CGPath;
    self.shapeLayer2.path = [UIBezierPath somePath].CGPath;
}

Question 2: Regardless of whether I implement the static shapes as CAShapeLayers or in drawRect: , is the best way to implement an animatable shape for this UIView a CAShapeLayer property that is implemented as the other CAShapeLayer s would be implemented above? 问题2:无论我是将静态形状实现为CAShapeLayers还是在drawRect: ,是实现此UIView的可动画形状的最佳方式, CAShapeLayer属性是否实现为其他CAShapeLayer将在上面实现? Creating a whole separate UIView class just for one animated shape and adding it as a subview to this view seems silly, so I'm thinking that a CAShapeLayer is the way to go to accomplish that. 为一个动画形状创建一个完整的单独的UIView类,并将其作为子视图添加到此视图中似乎很愚蠢,所以我认为CAShapeLayer是实现这一目标的方法。

I would appreciate any help with this very much! 非常感谢任何帮助!

You could do it either way, but using shape layers for everything would probably be cleaner and faster. 你可以这样做,但使用形状图层可能会更清洁,更快。 Shape layers have built-in animation support using Core Animation. 形状图层使用Core Animation具有内置动画支持。

It's typically faster to avoid implementing drawRect and instead let the system draw for you. 避免实现drawRect通常会更快,而是让系统为您绘制。

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

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