一、用法:
眾所周知,設置控件的圓角使用
layer
.
cornerRadius
屬性即可,但是這樣設置成的結果是4個邊角都是圓角類型。
利用班賽爾曲線畫角:
//
利用班賽爾曲線畫角
UIBezierPath
*bezierPath = [
UIBezierPath
bezierPathWithRoundedRect
:button.
bounds
byRoundingCorners
:(
UIRectCornerBottomLeft
|
UIRectCornerBottomRight
)
cornerRadii
:
CGSizeMake
(
10
,
10
)];
CAShapeLayer
*shapeLayer = [[
CAShapeLayer
alloc
]
init
];
shapeLayer.
frame
= button.
bounds
;
shapeLayer.
path
= bezierPath.
CGPath
;
button.
layer
.
mask
= shapeLayer;
關於設置指定位置控件圓角的枚舉:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0, //左上
UIRectCornerTopRight = 1 << 1, //右上
UIRectCornerBottomLeft = 1 << 2, //左下
UIRectCornerBottomRight = 1 << 3, //右下
UIRectCornerAllCorners = ~0UL //全角
};
我的用法如下:
UIBezierPath
*maskPathA = [
UIBezierPath
bezierPathWithRoundedRect
:
_smsCodeTFiled
.
bounds
byRoundingCorners
:
UIRectCornerBottomLeft
|
UIRectCornerTopLeft
corner Radii
:
CGSizeMake
(
self
.
smsCodeTFiled
.
bounds
.
size
.
height
/
2.0
,
self
.
smsCodeTFiled
.
bounds
.
size
.
height
/
2.0
)];
CAShapeLayer
*maskLayerA = [[
CAShapeLayer
alloc
]
init
];
maskLayerA.
frame
=
_smsCodeTFiled
.
bounds
;
maskLayerA.
path
= maskPathA.
CGPath
;
_smsCodeTFiled
.
layer
.
mask
= maskLayerA;
_smsCodeTFiled
.
layer
.
masksToBounds
=
YES
;
[
self
.
view
bringSubviewToFront
:
self
.
smsCodeLb
];
二、我的想法
這個用法主要用於在左邊有圓角,右邊沒有,或者相反的狀況,但是注意一下就是,當設置多個UIView時,要記得不要聲明相同的名稱,例如
UIBezierPath
*maskPath 后面聲明其他UIview的時候也這么寫
UIBezierPath
*maskPath 就會出錯了,記得不要重復聲明同一個名稱的對象就好。如果此方法沒有生效,很大可能是Xib文件加了約束導致此方法不能生效。
三、思考與行動
1.嘗試不用純代碼,利用Xib 能否使這某個UIView 指定位置設置圓角?
2.嘗試通過其他方法,來設置UIVIew指定位置圓角,你能想到幾種方法?感覺那種最好用?哪種最不消耗內存。