设置圆角的位置

废话不多说,直接上代码

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(60, 10, WIDTH-120, WIDTH-120);
    [button setTitle:@"圆角位置" forState:UIControlStateNormal];
    [button setConnerType:CONNERTYPE_TopLeftAndBottomLeftAndTopRight cornerRedius:(WIDTH-120)/2];
    button.backgroundColor=[[UIColor orangeColor]colorWithAlphaComponent:0.86];
    [self.view addSubview:button];
- (void)setConnerType:(CONNERTYPE)type cornerRedius:(double)redius {
    UIRectCorner corners;
    
    switch ( type )
    {
        case CONNERTYPE_All:
            corners = UIRectCornerAllCorners;
            break;
        case CONNERTYPE_BottomLeft:
            corners = UIRectCornerBottomLeft;
            break;
        case CONNERTYPE_BottomRight:
            corners = UIRectCornerBottomRight;
            break;
        case CONNERTYPE_TopLeft:
            corners = UIRectCornerTopLeft;
            break;
        case CONNERTYPE_TopRight:
            corners = UIRectCornerTopRight;
            break;
        case CONNERTYPE_BottomLeftAndBottomRight:
            corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
            break;
        case CONNERTYPE_BottomLeftAndTopLeft:
            corners = UIRectCornerBottomLeft | UIRectCornerTopLeft;
            break;
        case CONNERTYPE_BottomLeftAndTopRight:
            corners = UIRectCornerBottomLeft | UIRectCornerTopRight;
            break;
        case CONNERTYPE_TopLeftAndTopRight:
            corners = UIRectCornerTopLeft | UIRectCornerTopRight;
            break;
        case CONNERTYPE_TopLeftAndTopBottomRight:
            corners = UIRectCornerTopLeft | UIRectCornerBottomRight;
            break;
        case CONNERTYPE_BottomRightAndTopRight:
            corners = UIRectCornerTopRight | UIRectCornerBottomRight;
            break;
        case CONNERTYPE_BottomLeftAndBottomRightAndTopRight:
            corners = UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight;
            break;
        case CONNERTYPE_TopLeftAndBottomRightAndTopRight:
            corners = UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomRight;
            break;
        case CONNERTYPE_TopLeftAndBottomLeftAndBottomRight:
            corners = UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight;
            break;
        case CONNERTYPE_TopLeftAndBottomLeftAndTopRight:
            corners = UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerTopRight;
            break;
            default:
            corners = UIRectCornerAllCorners;
            break;
    }
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(redius, redius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame         = self.bounds;
    maskLayer.path          = maskPath.CGPath;
    self.layer.mask         = maskLayer;
}

/**
 半角位置

 - CONNERTYPE_All: 全角
 - CONNERTYPE_BottomLeft: 左下
 - CONNERTYPE_BottomRight: 右下
 - CONNERTYPE_TopLeft: 左上
 - CONNERTYPE_TopRight: 右上
 - CONNERTYPE_BottomLeftAndBottomRight: 左下和右下
 - CONNERTYPE_BottomLeftAndTopLeft: 左下和左上
 - CONNERTYPE_BottomLeftAndTopRight: 左下和右上
 - CONNERTYPE_TopLeftAndTopRight: 左上和右上
 - CONNERTYPE_TopLeftAndTopBottomRight: 左上和右下
 - CONNERTYPE_BottomRightAndTopRight: 右下和右上
 - CONNERTYPE_BottomLeftAndBottomRightAndTopRight: 缺左上
 - CONNERTYPE_TopLeftAndBottomRightAndTopRight: 缺左下
 - CONNERTYPE_TopLeftAndBottomLeftAndBottomRight: 缺右上
 - CONNERTYPE_TopLeftAndBottomLeftAndTopRight: 缺右下
 */
typedef NS_ENUM(NSInteger,CONNERTYPE)
{
    CONNERTYPE_All=0,
    CONNERTYPE_BottomLeft=1,
    CONNERTYPE_BottomRight,
    CONNERTYPE_TopLeft,
    CONNERTYPE_TopRight,
    CONNERTYPE_BottomLeftAndBottomRight,
    CONNERTYPE_BottomLeftAndTopLeft,
    CONNERTYPE_BottomLeftAndTopRight,
    CONNERTYPE_TopLeftAndTopRight,
    CONNERTYPE_TopLeftAndTopBottomRight,
    CONNERTYPE_BottomRightAndTopRight,
    CONNERTYPE_BottomLeftAndBottomRightAndTopRight,
    CONNERTYPE_TopLeftAndBottomRightAndTopRight,
    CONNERTYPE_TopLeftAndBottomLeftAndBottomRight,
    CONNERTYPE_TopLeftAndBottomLeftAndTopRight,
};

你可能感兴趣的:(设置圆角的位置)