前言
寫代碼的時(shí)候遇到一個(gè)問題允睹,需要給一個(gè)按鈕設(shè)置左邊兩個(gè)圓角棉圈,右邊為直角榕酒,網(wǎng)上找了一些資料胚膊,發(fā)現(xiàn)有些確實(shí)是可以自定義圓角的位置的。
比如:大部分資料都是想鹰,使用UIBezierPath和CAShapeLayer來設(shè)置view的mask
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120,10,80,80)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10,10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;
但是有一點(diǎn)紊婉,我在項(xiàng)目中是使用Masonry來添加約束的,前面代碼有效果的前提是:view的frame需要在添加之前就確定下來辑舷!喻犁,而Masonry則是使用block回調(diào)來約束的,所以:使用Masonry的UI代碼何缓,上面的是沒有效果的V !碌廓!
經(jīng)過查閱資料传轰,原來:
使用masonry的實(shí)質(zhì)還是調(diào)用了ios7以后的autolayout
,如果要更新frame谷婆,需要調(diào)用layoutIfNeeded
函數(shù)進(jìn)行布局路召,然后所約束的控件才會(huì)按照約束條件,生成當(dāng)前布局相應(yīng)的frame和bounds波材。這樣就可以利用這兩個(gè)屬性來進(jìn)行圖片圓角剪裁。而調(diào)用layoutIfNeeded的目的是讓系統(tǒng)調(diào)用layoutSubviews方法身隐,我們也可以直接在這個(gè)方法里獲取frame廷区,因?yàn)檫@時(shí)候開始layout subviews,Masonry已經(jīng)計(jì)算出了真實(shí)的frame贾铝。
所以隙轻,上面的代碼埠帕,如果使用Masonry,則要改成:
UIView *view2 = [[UIView alloc]init];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 40));
make.left.equalTo(self.view).offset(100);
make.top.equalTo(self.view).offset(100);
}];
//調(diào)用此方法,后面代碼才會(huì)起作用
[view2 layoutIfNeeded];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10,10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;
其實(shí)玖绿,實(shí)際中的我的代碼是這樣的:
self.plusBtn = [WDTools createButtonWithTitle:@"+" frame:CGRectZero target:self selector:@selector(plusAction)];
[self addSubview:self.plusBtn];
[self.plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self);
make.top.equalTo(self);
make.height.equalTo(self);
make.width.equalTo(self).multipliedBy(0.35);
}];
[self.plusBtn layoutIfNeeded];
UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:self.plusBtn.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init];
maskLayer2.frame = self.plusBtn.bounds;
maskLayer2.path = maskPath2.CGPath;
self.plusBtn.layer.mask = maskLayer2;
self.plusBtn.layer.borderColor = DDColorRandom.CGColor;
self.plusBtn.layer.borderWidth = 2;
但是效果呢敛瓷?
可以看出,我是想要給這個(gè)按鈕添加一個(gè)Border的斑匪,但是用了上述的方法呐籽,并不能達(dá)到我要的效果!J慈场狡蝶!
如果有知道的大神們,也請給個(gè)指點(diǎn)贮勃!謝謝贪惹!
結(jié)果
我就怒從心頭起,惡向膽邊生寂嘉,于是我就自己寫了一個(gè)Button的子類.
看效果吧奏瞬!
RoundedCornerButton *test = [[RoundedCornerButton alloc]init];
[test setTitle:@"-" forState:UIControlStateNormal];
test.direction = kCornerDirectionTopLeft | kCornerDirectionBottomLeft;
test.roundedCornerRadius = 5.f;
test.strokeWidth = 1.f;
test.strokeColor = DDColorRandom;
test.backgroundColor = DDColorRandom;
[test addTarget:self action:@selector(minusAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:test];
[test mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self);
make.height.equalTo(self);
make.width.mas_equalTo(40);
}];
顯然,我這里同時(shí)設(shè)置了邊框的顏色泉孩,和背景色硼端,但是這兩個(gè)是不會(huì)產(chǎn)生影響的。
typedef NS_ENUM(NSInteger,CornerDirection) {
kCornerDirectionTopLeft = 1 << 0,
kCornerDirectionTopRight = 1 << 1,
kCornerDirectionBottomLeft = 1 << 2,
kCornerDirectionBottomRight = 1 << 3,
};
@interface RoundedCornerButton : UIButton
//圓角方向
@property (assign, nonatomic) CornerDirection direction;
//圓角半徑
@property (assign, nonatomic) CGFloat roundedCornerRadius;
//邊線寬度
@property (assign, nonatomic) CGFloat strokeWidth;
//邊線顏色
@property (strong, nonatomic) UIColor *strokeColor;
@end
接口方面棵譬,不能再簡單显蝌,就不多說了,
這里傳到Github上,感覺有幫助的請給個(gè)star! 謝謝订咸!
PS
這里還有一個(gè)以前寫的類似的設(shè)置按鈕背景色為漸變色的曼尊,還有一個(gè)給View添加指定數(shù)量的border的,有需要的可以看一下脏嚷。