简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM