直奔主题看演示效果:
代码:
#import "ViewController.h"
#define ScreenWidth self.view.bounds.size.width
#define ScceenHeight self.view.bounds.size.height
@interface ViewController ()
@property (nonatomic,strong)UIButton * btn1,* btn2,* btn3 ,* btn4, * btn5;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat btnWidth = (ScreenWidth-30)/3;
CGFloat bigBtnHeight = btnWidth;
CGFloat smallBtnHeight = (bigBtnHeight-5)*0.5;
// btn1
_btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 50, btnWidth, bigBtnHeight)];
_btn1.backgroundColor = [UIColor redColor];
[_btn1 setTitle:@"btn1" forState:UIControlStateNormal];
[self.view addSubview:_btn1];
CGSize size = CGSizeMake(10, 10);
CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:_btn1.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:size];
maskLayer1.frame = _btn1.bounds;
maskLayer1.path = maskPath1.CGPath;
_btn1.layer.mask = maskLayer1;
// _btn4
_btn4 = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-10-btnWidth, 50, btnWidth, bigBtnHeight)];
_btn4.backgroundColor = [UIColor grayColor];
[_btn4 setTitle:@"btn4" forState:UIControlStateNormal];
[self.view addSubview:_btn4];
CAShapeLayer * maskLayer4 = [[CAShapeLayer alloc]init];
UIBezierPath * maskPath4 = [UIBezierPath bezierPathWithRoundedRect:_btn4.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:size];
maskLayer4.frame = _btn4.bounds;
maskLayer4.path = maskPath4.CGPath;
_btn4.layer.mask = maskLayer4;
// _btn2
_btn2 = [[UIButton alloc]initWithFrame:CGRectMake(btnWidth+15, 50, btnWidth, smallBtnHeight)];
_btn2.backgroundColor = [UIColor greenColor];
[_btn2 setTitle:@"btn2" forState:UIControlStateNormal];
[self.view addSubview:_btn2];
// _btn3
_btn3 = [[UIButton alloc]initWithFrame:CGRectMake(btnWidth+15, 50+5+smallBtnHeight, btnWidth, smallBtnHeight)];
_btn3.backgroundColor = [UIColor blueColor];
[_btn3 setTitle:@"btn3" forState:UIControlStateNormal];
[self.view addSubview:_btn3];
// btn5
_btn5 = [[UIButton alloc]initWithFrame:CGRectMake(50, 200, ScreenWidth-100, 100)];
_btn5.backgroundColor = [UIColor purpleColor];
[_btn5 setTitle:@"btn5" forState:UIControlStateNormal];
[self.view addSubview:_btn5];
CAShapeLayer * maskLayer5 = [[CAShapeLayer alloc]init];
UIBezierPath * maskPath5 = [UIBezierPath bezierPathWithRoundedRect:_btn5.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:size];
maskLayer5.frame = _btn5.bounds;
maskLayer5.path = maskPath5.CGPath;
_btn5.layer.mask = maskLayer5;
}
@end
其实,btn5的这种四个角都有圆角效果可以不用贝塞尔曲线写,直接用layer,如:
_btn5.layer.cornerRadius = 10;
_btn5.layer.masksToBounds = YES;
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:如何用贝塞尔曲线处理控件的边缘化