Masking a CALayer with another CALayer

后端 未结 4 805
灰色年华
灰色年华 2021-02-07 08:49

I\'m trying to make a donut shape with CALayers. One CALayer will be a large circle, the other one will be a smaller circle positioned in its center, masking it.

The la

4条回答
  •  逝去的感伤
    2021-02-07 09:20

    Indeed, as @David indicates the current (iOS 5.1) CALayer masks can't be reversed, which poses a problem if you want to use them to make a transparent hole a simple circular CALayer.

    What you can do to get a donut is make a circular CALayer's backgroundColor transparent, but give it a borderColor and a wide borderWidth. Here's the dunkin' code:

        CALayer *theDonut = [CALayer layer];
        theDonut.bounds = CGRectMake(0,0, radius, radius);
        theDonut.cornerRadius = radius/2;
        theDonut.backgroundColor = [UIColor clearColor].CGColor;
    
        theDonut.borderWidth = radius/5;
        theDonut.borderColor = [UIColor orangeColor].CGColor;
    
        [self.layer addSublayer:theDonut];
    

提交回复
热议问题