1、初始化UIButton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitleColor:[Toolkit hexColor:@"ffffff" forState:UIControlStateNormal];
[btn setTitle:@"我要留下" forState:UIControlStateNormal];
btn.backgroundColor = [Toolkit hexColor:@"fe4c00"];
[btn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
2、进行切圆角操作,使用UIBezierPath和CAShapeLayer切除需要的圆角
/*关键参数:byRoundingCorners
*UIRectCorner枚举:
*UIRectCornerTopRight(右上)、
*UIRectCornerBottomRight(右下)、
*UIRectCornerTopLeft(左上)、
*UIRectCornerBottomLeft(左下)
*/
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cancelBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.cancelBtn.bounds;
maskLayer.path = maskPath.CGPath;
self.cancelBtn.layer.mask = maskLayer;
注意:如果初始化UIButton或者使用Masonry布局,一开始按钮没有指定frame,此时bounds是CGRectZero,所以会使得mask不显示,因此要在UIView的layoutSubviews或者UIViewController的viewDidLayoutSubviews中进行切圆角操作才有效。
网友评论