0
votes

My question requires quite a bit of explanation. If you want to skip to the question, look for the bold "Question:" towards the end.

It's fairly easy to create a "circle wipe" animation in iOS or Mac OS using a Core Animation and masks.

One way is to install a CAShapeLayer as a mask on a view (typically an image view), and installing a circle in the shape layer, and animating the circle from 0 size to large enough to cover the whole image (r = sqrt(height^2 + width^2)

Another way is to use a radial CAGradientLayer as a mask, set up the center of the gradient as an opaque color, and have a sudden transition to clear just beyond the corners of the image. You then animate the "positions" values of the gradient layer to move the clear color in to the center of the image.

Both of these approaches create a sharp-edged "circle wipe" that causes the image to shrink to a point and disappear.

That effect looks like this:

enter image description here

The classic cinematic circle wipe animation has a feathered edge, however, rather than a sharp border. The outer edge of the circle is completely transparent. As you move in towards the center the image becomes more and more solid over a fairly short distance, and then the image is completely opaque from there into the center. As the circle shrinks, the thickness of the transition from transparent to opaque remains constant, and the circle stinks down to a point and disappears. (The mask is a circle is opaque in the middle and has a slightly blurred edge, and the radius of the circle shrinks to 0 while the thickness of the blurred edge remains constant.)

Here is an example of an old school circle wipe. This is technically an "Iris Out" where the transition is to black, but the idea is the same:

https://youtu.be/IqDhAW3TDR8?t=90

I have no idea how to achieve a circle wipe with a feathered edge using a shape layer as a mask. Shape layers are always sharp-edged. I could get fairly close by using a shape layer where the circle is stroked with 50% opacity, and the middle of the circle is filled with a color at 100% opacity, but that wouldn't get the smooth transition from transparent to opaque that I'm after.

Another approach I've tried is using a radial gradient with 3 colors in it. I set the inner colors as opaque, and the outer color as clear. I set the location of the outer color as > 1 (say 1.2), the middle location to 1, and the inner location to 0. The locations array contains [1.2, 1.0, 0] That makes the whole part of the mask that covers the image opaque, with a feathered transition to clear that happens past the edges of the image.

I then do an animation of the locations array in 2 steps. In the first step, I animate the locations array to [0, 0, 0.2]. That causes a band of feathering to move in from the corners and stop at 0.2 from the center of the image.

In the 2nd step, I animate the locations array from [0, 0, 0.2] to [0,0,0]. That causes an inner, transparent-to-opaque center to shrink to a point and disappear.

The effect is ok, and if I run it with a narrow blur band and a fast duration, it looks decent, but it's not perfect.

Here is what it looks like with a blur range of 0.2, and a duration of (I think) 0.25 seconds:

enter image description here

If I introduce a pause between the animation steps, however, you can see the imperfections in the effect:

enter image description here

Question: Is there a way to animate a circle with a small blur radius (5 points or so) from the full size of a view down to 0 using Core Animation?

The same question applies to other types of wipe animations as well: Keyhole, star, side wipe, box, and many others. For those, the radial gradient trick I'm using for a feathered circle wipe wouldn't work at all. (You create those specialized wipe animations using a CAShapeLayer that's the shape you want to animate, and then adjust it's size from large to small.

It would be easy to create a static blurred circle (or other shape) by first drawing a circle and then applying a Core Image blur filter to it, but there's no way to animate that in iOS, and the CI blur filter is too slow on iOS even if you could.

Here is a link to the project I used to create the animations in this post using the 3-color radial gradient I describe: https://github.com/DuncanMC/GradientView.git.

Edit:

Based On James' comment, I tried using a shape layer with a shadow on it as a mask. The effect is on the right track, but there is still a stark transition from the opaque part of the image to the mostly transparent part which is masked by the shadow layer. Even with a shadow opacity of 1.0 the shadow is still mostly transparent. Here's what it looks like:

enter image description here

Making the line color of the circle shape partly transparent helps some, and gives a transition between the opaque part of the image and the mostly transparent shadow. Here's the image above but stroking the shape layer at a line width of 2 points and an opacity of 0.6:

enter image description here

Edit #2: SOLVED!

James' idea of using a shadowPath is the solution. You don't want anything in your mask layer other than a shadowPath. If you do that you can use the shadowRadius property to control the amount of blurring, and smoothly animate a shadow path from a circle that fully encloses the view down to a vanishing point all in one step.

I've updated my github project at https://github.com/DuncanMC/GradientView.git to include both the radial gradient animation and the shadowPath animation for comparison.

Here is what the finished effect looks like, first with a 5 pixel blur radius:

ShadowPath based circle wipe with 5 pixel blur

And then with a 30 pixel blur radius:

enter image description here

1
Well, I was going to suggest animating a vignetting CIFilter. But then I stumbled over your objection that there were “imperfections” that I can’t see in what you’re doing already. I’m afraid the reader is left without a clear positive idea of what the goal would look like.matt
I'll try to find an example of a cinematic circle wipe transition to add to my question as an example of what I'm after. The Wiki article on wipe transitions (en.wikipedia.org/wiki/Wipe_(transition) shows several examples of wipe transitions that are what I'm after, but does not include a circle wipe as luck would have it.Duncan C
At a blur percentage setting of 1%, the animation looks pretty much exactly like a slightly feathered circle wipe. As I increase the blur percentage it looks less and less natural. I just pushed a new version that has controls to set the blur percentage, animation duration, and optionally add a delay between the first and second halves of the animation. Feel free to download it and play with it.Duncan C
I'm not sure exactly what you're looking for, but what about a CAShapelayer with an animated shadowPath?James P
That's a really good idea. I'll have to try that. I'd want a 0 shadow offset to create a halo around the shape.Duncan C

1 Answers

2
votes

You could use the shadowPath on a CAShapeLayer to create a circle with a blurred outline to use as the mask.

Create a shape layer with no fill or stroke (we only want to see the shadow) and add it as the layer mask to your view.

let shapeLayer = CAShapeLayer()
shapeLayer.shadowColor = UIColor.black.cgColor
shapeLayer.shadowOffset = CGSize(width: 0, height: 0)
shapeLayer.shadowOpacity = 1
shapeLayer.shadowRadius = 5
self.maskLayer = shapeLayer
self.layer.mask = shapeLayer

And animate it with a CABasicAnimation.

func show(_ show: Bool) {
    
    let center = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2)
    var newPath: CGPath
    if show {
        let radius = sqrt(self.bounds.height*self.bounds.height + self.bounds.width*self.bounds.width)/2 //assumes view is square
        newPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true).cgPath
    } else {
        newPath = UIBezierPath(arcCenter: center, radius: 0.01, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true).cgPath
    }
    
    let shadowAnimation = CABasicAnimation(keyPath: "shadowPath")
    shadowAnimation.fromValue = self.maskLayer.shadowPath
    shadowAnimation.toValue = newPath
    shadowAnimation.duration = totalDuration

    self.maskLayer.shadowPath = newPath
    self.maskLayer.add(shadowAnimation, forKey: "shadowAnimation")

}

As you can set any path for the shadow it should also work for other shapes (star, box etc.).