iOS開發之指定UIView的某幾個角為圓角

2022-08-01 16:51:15 字數 1365 閱讀 4305

如果需要將uiview的4個角全部都為圓角,做法相當簡單,只需設定其layer的cornerradius屬性即可(專案需要使用quartzcore框架)。而若要指定某幾個角(小於4)為圓角而別的不變時,這種方法就不好用了。

對於這種情況,stackoverflow上提供了幾種解決方案。其中最簡單優雅的方案,就是使用uibezierpath。下面給出一段示例**。

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

;

其中,

byroundingcorners:uirectcornerbottomleft | uirectcornerbottomright
指定了需要成為圓角的角。該引數是uirectcorner型別的,可選的值有:

* uirectcornertopleft

* uirectcornertopright

* uirectcornerbottomleft

* uirectcornerbottomright

* uirectcornerallcorners

從名字很容易看出來代表的意思,使用「|」來組合就好了。

iOS開發之指定UIView的某幾個角為圓角

如果需要將uiview的4個角全部都為圓角,做法相當簡單,只需設定其layer的cornerradius屬性即可 專案需要使用quartzcore框架 而若要指定某幾個角 小於4 為圓角而別的不變時,這種方法就不好用了。對於這種情況,stackoverflow上提供了幾種解決方案。其中最簡單優雅的方...

iOS開發之指定UIView的某幾個角為圓角

如果需要將uiview的4個角全部都為圓角,做法相當簡單,只需設定其layer的cornerradius屬性即可 專案需要使用quartzcore框架 而若要指定某幾個角 小於4 為圓角而別的不變時,這種方法就不好用了。對於這種情況,stackoverflow上提供了幾種解決方案。其中最簡單優雅的方...

iOS開發之指定UIView的某幾個角為圓角

如果需要將uiview的4個角全部都為圓角,做法相當簡單,只需設定其layer的cornerradius屬性即可 專案需要使用quartzcore框架 而若要指定某幾個角 小於4 為圓角而別的不變時,這種方法就不好用了。對於這種情況,stackoverflow上提供了幾種解決方案。其中最簡單優雅的方...