Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping CAShapeLayer Mask Static During Animation

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.

like image 494
Samuel Iglesias Avatar asked Feb 21 '23 10:02

Samuel Iglesias


1 Answers

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

like image 85
Nikolai Ruhe Avatar answered Mar 06 '23 16:03

Nikolai Ruhe