最近有个ui的同事抱怨说她公司的前段开发动不动就让她切个小图,理由是实现不了,像下面这种小功能:
其实这也是按钮的倒圆角,并不难。上代码:
.h
#import <UIKit/UIKit.h>
@interface YMBtnCorner : UIButton
//左上角
- (void)ym_rectCorner:(CGFloat)radius conrners:(UIRectCorner)corners;
@end
.m
#import "YMBtnCorner.h"
@implementation YMBtnCorner
- (void)ym_rectCorner:(CGFloat)radius conrners:(UIRectCorner)corners
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
@end
用法:
//左上
[self.topLeftCorner ym_rectCorner:10.0 conrners:UIRectCornerTopLeft];
//左下
[self.bottomLeftcorner ym_rectCorner:10 conrners:UIRectCornerBottomLeft];
//右上
[self.topRightCorner ym_rectCorner:10 conrners:UIRectCornerTopRight];
//右下
[self.bottomRightCorner ym_rectCorner:10 conrners:UIRectCornerBottomRight];
//四个角
[self.allCorner ym_rectCorner:10 conrners:UIRectCornerAllCorners];
//左右上角
[self.topLeftRightCorner ym_rectCorner:10 conrners:UIRectCornerTopRight | UIRectCornerTopLeft];
//左右下角
[self.bottomLeftRightCorner ym_rectCorner:10 conrners:UIRectCornerBottomRight | UIRectCornerBottomLeft];
//左上右下
[self.topLeftBottomRight ym_rectCorner:10 conrners:UIRectCornerTopLeft | UIRectCornerBottomRight];
//左下右上
[self.bottomLeftTopRight ym_rectCorner:10 conrners:UIRectCornerBottomLeft | UIRectCornerTopRight];
//左边
[self.leftCorner ym_rectCorner:10 conrners:UIRectCornerBottomLeft | UIRectCornerTopLeft];
//右边
[self.rightCorner ym_rectCorner:10 conrners:UIRectCornerBottomRight | UIRectCornerTopRight];