-1

I am working on a prototype for building a DFD builder. This will have a palette of different objects for drawing DFD. (Rectangle boxes, arrows, Ellipses,circles).

I initially planned to use plain CAShapeLayer's to create objects and add it to the superview's layer (with help of CGPaths or UIBezierPath), so that I will reduce the memory footprint of the application.

Then I realized that using CAShapeLayer and adding them directly in super view's layer makes me to handle touches and use hit test to find the CAShapeLayer that was interacted by the user.

So I am planning to create a Custom UIView backed by CAShapeLayer so that I do not bother about handling the touches and spotting the specific object interacted by user.

Is this approach better than using plain CAShapeLayer? and if yes, please provide the reasons?

UPDATE:

@matt I did a memory testing and the results show that using CAShapeLayer will save memory

enter image description here

RK-
  • 12,099
  • 23
  • 89
  • 155
  • "I did a memory testing and the results show that using CAShapeLayer will save memory." They show nothing of the sort, and in any case those numbers are tiny and are swamped by the layer bitmap backing which is not shown in Instruments. – matt Jul 19 '14 at 05:11
  • ok I was assuming that they directly indicate the memory usage. – RK- Jul 22 '14 at 09:35

1 Answers1

4

Neither approach is "better". A layer cannot exist without a view, and you can hit-test a layer just as well as you can hit-test a view. If you have multiple tappable drawn objects then it is going to be simplest if they are all separate layers so that you don't have to calculate which one was tapped; but that does not mean that they all need to be separate views.

As for "memory footprint", you are optimizing prematurely, a cardinal sin. Have you actual evidence that 100 layers (let's say) takes up less "memory footprint" than 100 views? Unless you do, you can't use that as a decision criterion.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I dont have an evidence as to the foot print of UIView vs CAShapeLayer, but it was hunch on my part because by default a UIView is backed by a CALayer and having a CAShapeLayer alone will save some memory because you having a UIView should consume some more memory.. – RK- Jul 18 '14 at 05:19
  • //If you have multiple tappable drawn objects then it is going to be simplest if they are all separate layers so that you don't have to calculate which one was tapped; //By this do you mean each instance belonging to a different Class? – RK- Jul 18 '14 at 05:25