I have a pretty basic question: How do you choose between using UIView
and CAShapeLayer
when you want to draw shapes (I'm not talking about textfields
, switches
or other controls, just drawing)?
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.
Then, when does using a UIView
make sense, and when does using CAShapeLayer
make sense?
Is CAShapeLayer
faster? Is UIKit
more optimised for gesture recognition or user interaction in general?
To give you more context, here's what I was trying to do when this question came up:
I want these red circles to rotate around the center circle. However, the user should be able to tap on the red circles while they are rotating.
Here, I see two main options (there may be more) to add one of those red circles:
- Create a
UIView
, manipulate its layer's cornerRadius and rotate it usingCGAffineTransform
. - Create a
CAShapeLayer
with aUIBezierPath
. I could rotate it usingCATransform3D
The only problem I have here is the user interaction
. As it's constantly moving (rotating), I'd have to access the correct frame. I can do that using the presentation layer
(which I think UIView
is also using under the hood).
At this point, I'm not sure whether to use UIView
s or CAShapeLayer
s. Also, I'm not sure if animating it this way is the correct way in this case. There may be better options that will also erase the question about which one to use.
Thanks for your thoughts about this.