1. 程式人生 > >UIButton只設置一個角為圓角

UIButton只設置一個角為圓角

、、 

1.建立一個按鈕

UIButton *btn=[[UIButtonalloc] initWithFrame:CGRectMake(10,70, 100, 100)];

    btn.backgroundColor=[UIColorredColor];

    [self.viewaddSubview:btn];

2.為了使按鈕只有一個角或者兩個角為圓角,可以用貝塞爾曲線和CAShapeLayer來完成

CAShapeLayer *mask=[CAShapeLayerlayer];

    UIBezierPath * path=                              [UIBezierPath

bezierPathWithRoundedRect:btn.boundsbyRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRightcornerRadii:CGSizeMake(15,10)];

    mask.path=path.CGPath;//從貝塞爾曲線獲取到形狀

    mask.frame=btn.bounds;

//    mask.backgroundColor=[UIColor blueColor].CGColor;

//    mask.lineWidth=5;

//    mask.fillColor=[UIColor blueColor].CGColor;

//    mask.strokeColor=[UIColor yellowColor].CGColor;

//    mask.lineCap=kCALineCapSquare;

3.但是我怎麼修改CAShapeLayer 的顏色都沒用,他不能在圓角外面加一個layer,後來又查了一下,在在外面加一層layer就可以顯示了

    CAShapeLayer *borderLayer=[CAShapeLayerlayer];

    borderLayer.path    =   path.CGPath;

    borderLayer.fillColor  = [UIColorclearColor

].CGColor;

    borderLayer.strokeColor    = [UIColoryellowColor].CGColor;

    borderLayer.lineWidth      =4;

    borderLayer.frame=btn.bounds;

    btn.layer.mask=mask;

    [btn.layeraddSublayer:borderLayer];