Keeping CAShapeLayer Mask Static During Animation

core-animation, ios

Solution

Put the image view into an additional plain UIView superview. Set the layer mask on the superview. Animate the image view.

Problem

I'm trying to rotate a UIImageView within a circular mask (which is set with myImageView.layer.mask). The problem is that any time I attempt to rotate said UIImageView, the mask rotates with it. Here is some code. Setting up the mask: ``` UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, - 169) radius:191 startAngle:0.0 endAngle:2*M_PI clockwise:YES]; CAShapeLayer *shapeLayer = [CAShapeLayer layer]; [shapeLayer setFrame:myImageView.bounds]; [shapeLayer setPath:[path CGPath]]; myImageView.layer.mask = shapeLayer; ``` Animation: ``` [UIView animateWithDuration:kRotationTime animations:^(void){ myImageView.transform = CGAffineTransformMakeRotation(angle); }]; ``` And that's basically all there is to it. A UIImageView rotating about its own center within a fixed-position circular mask--that's the desired effect. Perhaps there is another way to structure the view hierarchy so that myImageView and the mask are siblings, but I haven't had luck going that route.

Original source