I've created a circular animation using CAShapeLayer and masking. Here is my code:
- (void) maskAnimation{
animationCompletionBlock theBlock;
imageView.hidden = FALSE;//Show the image view
CAShapeLayer *maskLayer = [CAShapeLayer layer];
CGFloat maskHeight = imageView.layer.bounds.size.height;
CGFloat maskWidth = imageView.layer.bounds.size.width;
CGPoint centerPoint;
centerPoint = CGPointMake( maskWidth/2, maskHeight/2);
//Make the radius of our arc large enough to reach into the corners of the image view.
CGFloat radius = sqrtf(maskWidth * maskWidth + maskHeight * maskHeight)/2;
//Don't fill the path, but stroke it in black.
maskLayer.fillColor = [[UIColor clearColor] CGColor];
maskLayer.strokeColor = [[UIColor blackColor] CGColor];
maskLayer.lineWidth = 60;
CGMutablePathRef arcPath = CGPathCreateMutable();
//Move to the starting point of the arc so there is no initial line connecting to the arc
CGPathMoveToPoint(arcPath, nil, centerPoint.x, centerPoint.y-radius/2);
//Create an arc at 1/2 our circle radius, with a line thickess of the full circle radius
CGPathAddArc(arcPath,
nil,
centerPoint.x,
centerPoint.y,
radius/2,
3*M_PI/2,
-M_PI/2,
NO);
maskLayer.path = arcPath;//[aPath CGPath];//arcPath;
//Start with an empty mask path (draw 0% of the arc)
maskLayer.strokeEnd = 0.0;
CFRelease(arcPath);
//Install the mask layer into out image view's layer.
imageView.layer.mask = maskLayer;
//Set our mask layer's frame to the parent layer's bounds.
imageView.layer.mask.frame = imageView.layer.bounds;
//Create an animation that increases the stroke length to 1, then reverses it back to zero.
CABasicAnimation *swipe = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
swipe.duration = 5;
swipe.delegate = self;
[swipe setValue: theBlock forKey: kAnimationCompletionBlock];
swipe.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
swipe.fillMode = kCAFillModeForwards;
swipe.removedOnCompletion = NO;
swipe.autoreverses = YES;
swipe.toValue = [NSNumber numberWithFloat: 1.0];
[maskLayer addAnimation: swipe forKey: @"strokeEnd"];
}
This is my background image:
This what is looks like when I run the animation:
But what I want, the arrow head is missing how to add this?
UPDATE
See my other answer for a solution that doesn't have glitches.
ORIGINAL
This is a fun little problem. I don't think we can solve it perfectly with just Core Animation, but we can do pretty well.
We should set up the mask when the view is laid out, so we only have to do it when the image view first appears or when it changes size. So let's do it from
viewDidLayoutSubviews
:Here,
arrowLayer
is an instance variable, so I can animate the layer.To actually create the arrow-shaped layer, I need some constants:
Now I can create the layer. Since there's no “arrow-shaped” line end cap, I have to make a path that outlines the whole path, including the pointy tip:
If we do all that, it looks like this:
Now, we want the arrow to go around, so we apply a rotation animation to the mask:
When we tap the Go button, it looks like this:
That's not right, of course. We need to clip the arrow tail. To do that, we need to apply a mask to the mask. We can't apply it directly (I tried). Instead, we need an extra layer to act as the image view's mask. The hierarchy looks like this:
The new ring layer will be just like your original attempt to draw the mask: a single stroked ARC segment. We'll set up the hierarchy by rewriting
setUpMask
:We now have another ivar,
ringLayer
, because we'll need to animate that too. ThearrowLayerWithFrame:
method is unchanged. Here's how we create the ring layer:Note that we're setting the
strokeStart
to 1, instead of setting thestrokeEnd
to 0. The stroke end is at the tip of the arrow, and we always want the tip to be visible, so we leave it alone.Finally, we rewrite
goButtonWasTapped
to animate the ring layer'sstrokeStart
(in addition to animating the arrow layer's rotation):The end result looks like this:
It's still not perfect. There's a little wiggle at the tail and sometimes you get a column of blue pixels there. At the tip you also sometimes get a whisper of a white line. I think this is due to the way Core Animation represents the arc internally (as a cubic Bezier spline). It can't perfectly measure the distance along the path for
strokeStart
, so it approximates, and sometimes the approximation is off by enough to leak some pixels. You can fix the tip problem by changingkEndRadians
to this:And you can eliminate the blue pixels from the tail by tweaking the
strokeStart
animation endpoints:But you'll still see the tail wiggling:
If you want to do better than that, you can try actually recreating the arrow shape on every frame. I don't know how fast that will be.
Since my other answer (animating two levels of masks) has some graphics glitches, I decided to try redrawing the path on every frame of animation. So first let's write a
CALayer
subclass that's likeCAShapeLayer
, but just draws an arrow. I originally tried making it a subclass ofCAShapeLayer
, but I could not get Core Animation to properly animate it.Anyway, here's the interface we're going to implement:
The
startRadians
property is the position (in radians) of the end of the tail. ThelengthRadians
is the length (in radians) from the end of the tail to the tip of the arrowhead. TheheadLengthRadians
is the length (in radians) of the arrowhead.We also reproduce some of the properties of
CAShapeLayer
. We don't need thelineCap
property because we always draw a closed path.So, how do we implement this crazy thing? As it happens,
CALayer
will take care of storing any old property you want to define on a subclass. So first, we just tell the compiler not to worry about synthesizing the properties:But we need to tell Core Animation that we need to redraw the layer if any of those properties change. To do that, we need a list of the property names. We'll use the Objective-C runtime to get a list so we don't have to retype the property names. We need to
#import <objc/runtime.h>
at the top of the file, and then we can get the list like this:Now we can write the method that Core Animation uses to find out which properties need to cause a redraw:
It also turns out that Core Animation will make a copy of our layer in every frame of animation. We need to make sure we copy over all of these properties when Core Animation makes the copy:
We also need to tell Core Animation that we need to redraw if the layer's bounds change:
Finally, we can get to the nitty-gritty of drawing the arrow. First, we'll change the graphic context's origin to be at the center of the layer's bounds. Then we'll construct the path outlining the arrow (which is now centered at the origin). Finally, we'll fill and/or stroke the path as appropriate.
Moving the origin to the center of our bounds is trivial:
Constructing the arrow path is not trivial. First, we need to get the radial position at which the tail starts, the radial position at which the tail ends and the arrowhead starts, and the radial position of the tip of the arrowhead. We'll use a helper method to compute those three radial positions:
Then we need to figure out the radius of the inside and outside arcs of the arrow, and the radius of the tip:
We also need to know whether we're drawing the outer arc in a clockwise or counterclockwise direction:
The inner arc will be drawn in the opposite direction.
Finally, we can construct the path. We move to the tip of the arrowhead, then add the two arcs. The
CGPathAddArc
call automatically adds a straight line from the path's current point to the starting point of the arc, so we don't need to add any straight lines ourselves:Now let's figure out how to compute those three radial positions. This would be trivial, except we want to be graceful when the head length is larger than the overall length, by clipping the head length to the overall length. We also want to let the overall length be negative to draw the arrow in the opposite direction. We'll start by picking up the start position, the overall length, and the head length. We'll use a helper that clips the head length to be no larger than the overall length:
Next we compute the radial position where the tail meets the arrowhead. We do so carefully, so that if we clipped the head length, we compute exactly the start position. This is important so that when we call
CGPathAddArc
with the two positions, it doesn't add an unexpected arc due to floating-point rounding.Finally we compute the radial position of the tip of the arrowhead:
We need to write the helper that clips the head length. It also needs to ensure that the head length has the same sign as the overall length, so the computations above work correctly:
To draw the path in the graphics context, we need to set the filling and stroking parameters of the context based on our properties, and then call
CGContextDrawPath
:We fill the path if we were given a fill color:
We stroke the path if we were given a stroke color and a line width:
The end!
So now we can go back to the view controller and use an
ArrowLayer
as the image view's mask:And we can just animate the
lengthRadians
property from 0 to 2 π:and we get a glitch-free animation:
I profiled this on my iPhone 4S running iOS 6.0.1 using the Core Animation instrument. It seems to get 40-50 frames per second. Your mileage may vary. I tried turning on the
drawsAsynchronously
property (new in iOS 6) but it didn't make a difference.I've uploaded the code in this answer as a gist for easy copying.
Unfortunately, there are no options in path drawing to have a pointed line cap like the one you describe (options are available using
CAShapeLayer
'slineCap
property, just not the one you need).You will have to draw the path boundary yourself and fill it, instead of relying on the width of the stroke. This means 3 lines and 2 arcs, which should be manageable, although not as straightforward as what you tried to do.