Rounded UIView using CALayers - only some corners - How?

后端 未结 14 2231
南方客
南方客 2020-11-22 13:53

In my application - there are four buttons named as follows:

  • Top - left
  • Bottom - left
  • Top - right
  • Bottom - right

Abov

14条回答
  •  不知归路
    2020-11-22 14:13

    Wrapping up Stuart's answer, you can have rounding corner method as the following:

    @implementation UIView (RoundCorners)
    
    - (void)applyRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
    
        self.layer.mask = maskLayer;
    }
    
    @end
    

    So to apply rounding corner, you simply do:

    [self.imageView applyRoundCorners:UIRectCornerTopRight|UIRectCornerTopLeft radius:10];
    

提交回复
热议问题