项目中需要设置这种button ,两个button一个左边是圆角一个右边是圆角。这个?裁张图省事的搞法。贝塞尔绘制这个好像难搞点哈。

1 常规圆角操作

    button.layer.cornerRadius = 圆角大小;

    button.layer.masksToBounds = YES;或

    button.clipsToBounds = YES;

这个会得到一个圆或者同时设置四个角的圆角大小。


2  设置任意一角为圆角 

    UIButton * button =[UIButton buttonWithType:UIButtonTypeSystem];

    [self.view addSubview:button];

    button.frame=CGRectMake(100, 100, 100, 40);

//    button.backgroundColor=[UIColor blueColor];

    

    UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];

    CAShapeLayer *maskLayer=[[CAShapeLayer alloc]init];

    maskLayer.frame=button.bounds;

    maskLayer.path=maskPath.CGPath;

    button.layer.mask=maskLayer;

    

    button.layer.borderWidth = 2.0f;

    button.layer.borderColor =[UIColor redColor].CGColor;



加上背景色后 出现了间断。这个....  原因加上的边框被裁掉了。

3 最后的解决办法


typedef NS_OPTIONS(NSUInteger, UIRectCorner) {

    UIRectCornerTopLeft     = 1 << 0,

    UIRectCornerTopRight    = 1 << 1,

    UIRectCornerBottomLeft  = 1 << 2,

    UIRectCornerBottomRight = 1 << 3,

    UIRectCornerAllCorners  = ~0UL

};


注意下这个枚举 ,分别对应你要设置的哪个角!根据需求选


-( void)ctreateBtn{

 UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 74, 200, 40)];

    [btn setBackgroundColor:[UIColor greenColor]];

    [btn setTitle:@"记录" forState:(UIControlStateNormal)];

  [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

[btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn]; //需要先添加

    

    //设置圆角

    CGFloat borderWidth = 1.0;

    CGFloat radius = 20.0f;

    

   UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(radius, radius)];

    CAShapeLayer * mask  = [[CAShapeLayer alloc] init];

    mask.path = path.CGPath;

    btn.layer.mask = mask;

    

    CALayer * temp = [CALayer layer];

    [temp setBackgroundColor:[UIColor whiteColor].CGColor];

    temp.frame = CGRectMake(borderWidth, borderWidth, btn.bounds.size.width - borderWidth * 2, btn.bounds.size.height - borderWidth * 2);

    

    UIBezierPath * subPath = [UIBezierPath bezierPathWithRoundedRect:temp.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(radius - borderWidth, radius - borderWidth)];

    CAShapeLayer * subMask  = [[CAShapeLayer alloc] initWithLayer:temp];

    subMask.path = subPath.CGPath;

    temp.mask = subMask;

    

    [btn.layer addSublayer:temp];

}



 //点击事件 及 切换边框线颜色   

-(void)btnClickAction:(UIButton*)btn{

    


    btn.selected = !btn.selected;


    if (btn.selected) {

           [btn setBackgroundColor:[UIColor greenColor]];

    } else {

           [btn setBackgroundColor:[UIColor redColor]];

    }

 

}



Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐