button的圆角设置

设置button的圆角时可以选者指定4个角中的一个角,设置成圆角,可以用来实现如下的效果

804A353A-5428-4F72-AC2F-B04CF8CEFB48.png

设置左上角和左下角为圆角

 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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容