This wouldn't be that hard. Yes, using a CAShapeLayer
animation would be the way to go.
I would make the non-changing part of the image into a bitmap, and then draw your shape layer animation on top.
The trick with shape layer animation is that the shape you use needs to have the same number of control points for all the parts of your animation.
In the case of your tail animation you should be able to map it out as a set of quadratic bezier curves. You could probably draw the tail as a single thick path with a rounded end cap. (kCGLineCapRound
). You'd make the starting curve a quadratic bezier curve with the starting point down at the beginning of the straight part of the tail, the next control point slightly to the right and below the curved tip, the next control point above and to the left of the first control point but still below and to the right of the tip, and the last control point at the tip.
The ending curve would have the first control point at the same place on the base of the tail, the second control point at about the top of the curve and to the left, the next control point above the curve and about 2/3 of the way between the base point and the tip, and the last point at the tip of the tail.
You might want to play with the path tool in Adobe Photoshop (or GIMP) and use it to create the starting and ending tail curve shapes using a single quadratic Bezier curve, then write down the control point positions that you use and enter them into your code.
You'd create a CABasicAnimation
of the CAShapeLayer
where you set the fromValue to the starting CGPath and the toValue to the ending CGPath. Just make sure that the starting and ending paths have the same number of control point.
I have a project called RandomBlobs (link) on Github that shows how to animate a curve using CGPath
s and CABasicAnimation
s, but it is written in Objective-C, and instead of using quadratic bezier curves, it creates another kind of a curve called a Catmull-Rom spline. quadratic bezier curves are actually much simpler to set up than Catcall-Rom splines however, and the idea behind animating changes to a CAShapeLayer
using CABasicAnimation
should be fairly easy to translate from Objective-C to Swift if you've worked with CAShapelayer
s before. You could also use a series of Catmull-Rom splines to get the tail animation you're after. The advantage of Catmull-Rom splines is that all the control points are on the curve.