I want to create a diagram application, I can create some shapes. Every shape can be moved in the canvas. What is the best way to implement it? Now I know is just two way:
Is there any other good way? The first is too complicated. The second seems has bad performance ?
Either will work, but each have pros and cons. Specifically:
UIView
: This approach would require you to create a CALayer
for each shape and then do your own hit-testing and finger-dragging when the shape is moved. This approach will perform much better if you have many shapes (be sure to use an indexed lookup to do hit testing rather than an O(N) search) since CALayer
s are lightweight.
UIView
via a single call to drawRect:
. This will perform extremely poorly, especially when you are move the shapes during a drag, and as you indicate, is very complicated to implement wellUIView
Per Shape: This approach is very easy to program, as you don't have to do the hit-testing and the touch get's sent to the shape being touched. This approach will perform well if you have a few shapes (<30, in my experience). If you have a large number of shapes, you start to see issues with frame rate.