给控件画圆角的方法有很多,今天我们就介绍一下怎样用使用CAShapeLayer和UIBezierPath设置圆角。
#define SWidth [UIScreen mainScreen].bounds.size.width
#define SHeight [UIScreen mainScreen].bounds.size.height
UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:playBtn];
playBtn.frame = CGRectMake((SWidth - 100) / 2, (SHeight - 50) / 2, 100, 50);
UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:_playBtn.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = playBtn.bounds;
maskLayer.path = maskPath.CGPath;
playBtn.layer.mask = maskLayer;
[playBtn setTitle:@"视频" forState:UIControlStateNormal];
playBtn.backgroundColor = [UIColor blueColor];
使用这种方法设置圆角,对内存的消耗最少,而且渲染快速。