设置控件单个或某几个角为圆角的实现

来源:互联网 发布:mac唇膏专柜 编辑:程序博客网 时间:2024/04/24 01:22

-(void)changeButtonStyle:(UIButton*)button style:(NSInteger)style{

    UIRectCorner corners;

    switch (style) {

        case 0:

            corners = UIRectCornerBottomLeft | UIRectCornerTopLeft;

            break;

        case 1:

            corners = UIRectCornerTopRight | UIRectCornerBottomRight;

            break;

        default:

            corners = UIRectCornerAllCorners;

            break;

    }

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds

                                                   byRoundingCorners:corners

                                                         cornerRadii:CGSizeMake(5.0, 5.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];

    maskLayer.frame         = button.bounds;

    maskLayer.path          = maskPath.CGPath;

    button.layer.mask         = maskLayer;

}


不仅仅是button,所有的view都可以进行类似设置。


UIRectCornerTopLeft、 UIRectCornerBottomLeft、UIRectCornerTopRight、UIRectCornerBottomRight分别对应左上、左下、右上、右下,根据实际需要,进行或运算,生成所需的UIRectCorner。

0 0
原创粉丝点击