设置button的圆角时可以选者指定4个角中的一个角,设置成圆角,可以用来实现如下的效果
设置左上角和左下角为圆角
UIBezierPath *leftPath = [UIBezierPath bezierPathWithRoundedRect:self.midOneBtn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(TRANS_VALUE2(10.0f), TRANS_VALUE2(10.0f))];
CAShapeLayer *leftLayer = [[CAShapeLayer alloc] init];
leftLayer.frame = self.midOneBtn.bounds;
leftLayer.path = leftPath.CGPath;
self.midOneBtn.layer.mask = leftLayer;
指定哪个角为圆角的UIRectCorner
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
图中的实现具体代码
.h
@property (nonatomic, strong) UIButton * midOneBtn; //自提
@property (nonatomic, strong) UIButton * midTwoBtn; //配送
.m
self.midOneBtn = [[UIButton alloc] init];
self.midOneBtn.frame = CGRectMake(TRANS_VALUE2(27.0f), TRANS_VALUE2(188.0f), TRANS_VALUE2(160.5f), TRANS_VALUE2(38.0f));
[self.midOneBtn setTitle:@"自提" forState:UIControlStateNormal];
[self.midOneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.midOneBtn.backgroundColor = [UIColor redColor];
[self.midOneBtn addTarget:self action:@selector(midOneAction) forControlEvents:UIControlEventTouchDown];
[self.backView addSubview:self.midOneBtn];
UIBezierPath *leftPath = [UIBezierPath bezierPathWithRoundedRect:self.midOneBtn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(TRANS_VALUE2(10.0f), TRANS_VALUE2(10.0f))];
CAShapeLayer *leftLayer = [[CAShapeLayer alloc] init];
leftLayer.frame = self.midOneBtn.bounds;
leftLayer.path = leftPath.CGPath;
self.midOneBtn.layer.mask = leftLayer;
self.midTwoBtn = [[UIButton alloc] init];
self.midTwoBtn.frame = CGRectMake(TRANS_VALUE2(187.5f), TRANS_VALUE2(188.0f), TRANS_VALUE2(160.0f), TRANS_VALUE2(38.0f));
[self.midTwoBtn setTitle:@"配送" forState:UIControlStateNormal];
[self.midTwoBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.midTwoBtn.backgroundColor = [UIColor whiteColor];
[self.midTwoBtn addTarget:self action:@selector(midTwoBtnAction) forControlEvents:UIControlEventTouchDown];
[self.backView addSubview:self.midTwoBtn];
UIBezierPath *rightPath = [UIBezierPath bezierPathWithRoundedRect:self.midTwoBtn.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(TRANS_VALUE2(10.0f), TRANS_VALUE2(10.0f))];
CAShapeLayer *rightLayer = [[CAShapeLayer alloc] init];
rightLayer.frame = self.midTwoBtn.bounds;
rightLayer.path = rightPath.CGPath;
self.midTwoBtn.layer.mask = rightLayer;