iOS 設定View任意角為圓角

2021-08-28 12:03:44 字數 3428 閱讀 1091

通過**設定uiview的任意乙個角為圓角

先建立rectcorner 繼承自uiview  

.h**

#import

@inte***ce rectcorner : uiview

* 頂部圓角

- (void)setcornerontop;

*  底部圓角

- (void)setcorneronbottom;

*  全部圓角

- (void)setallcorner;

@end

.m**

#import "rectcorner.h"

@implementation rectcorner

- (void)setcornerontop {

uibezierpath *maskpath;

maskpath = [uibezierpath bezierpathwithroundedrect:self.bounds

byroundingcorners:(uirectcornertopleft | uirectcornertopright)

cornerradii:cgsizemake(8.0, 8.0)];

cashapelayer *masklayer = [[cashapelayer alloc] init];

masklayer.frame = self.bounds;

masklayer.path = maskpath.cgpath;

self.layer.mask = masklayer;

- (void)setcorneronbottom {

uibezierpath *maskpath;

maskpath = [uibezierpath bezierpathwithroundedrect:self.bounds

byroundingcorners:(uirectcornerbottomleft | uirectcornerbottomright)

cornerradii:cgsizemake(8.0, 8.0)];

cashapelayer *masklayer = [[cashapelayer alloc] init];

masklayer.frame = self.bounds;

masklayer.path = maskpath.cgpath;

self.layer.mask = masklayer;

- (void)setallcorner {

uibezierpath *maskpath;

maskpath = [uibezierpath bezierpathwithroundedrect:self.bounds

cornerradius:8.0];

cashapelayer *masklayer = [[cashapelayer alloc] init];

masklayer.frame = self.bounds;

masklayer.path = maskpath.cgpath;

self.layer.mask = masklayer;

- (void)setnonecorner {

self.layer.mask = nil;

@end

使用方法

#import "viewcontroller.h"

#import "rectcorner.h"

@inte***ce viewcontroller ()

@property (nonatomic,strong)rectcorner *rc;

@end

@implementation viewcontroller

- (void)viewdidload {

[super viewdidload];

//四角全不為圓角

rectcorner *rc = [[rectcorner alloc]init];

rc.backgroundcolor = [uicolor magentacolor];

rc.frame = cgrectmake(100, 100, 100, 100);

[self.view addsubview:rc];

[rc setallcorner];

//底部兩個角為圓角

rectcorner *rc1 = [[rectcorner alloc]init];

rc1.backgroundcolor = [uicolor magentacolor];

rc1.frame = cgrectmake(100, 220, 100, 100);

[self.view addsubview:rc1];

[rc1 setcorneronbottom];

//頂部兩個角為圓角

rectcorner *rc2 = [[rectcorner alloc]init];

rc2.backgroundcolor = [uicolor magentacolor];

rc2.frame = cgrectmake(100, 350, 100, 100);

[self.view addsubview:rc2];

[rc2 setcorneronbottom];

//任意角為圓角

uiview *bgview = [[uiview alloc]init];

bgview.frame = cgrectmake(220, 200, 50, 50);

bgview.backgroundcolor = [uicolor purplecolor];

bgview.userinteractionenabled = yes;

[self.view addsubview:bgview];

uirectcornertopleft 上左

uirectcornertopright上右

uirectcornerbottomright下右

uirectcornerbottomright下右

uibezierpath *maskpath = [uibezierpath bezierpathwithroundedrect:bgview.bounds byroundingcorners:uirectcornertopleft | uirectcornerbottomright cornerradii:cgsizemake(10, 10)];

cashapelayer *masklayer = [[cashapelayer alloc] init];

masklayer.frame = bgview.bounds;

masklayer.path = maskpath.cgpath;

bgview.layer.mask = masklayer;

@end

設定任意某個角為圓角

原文 設定控制項圓角,只設定上面兩個角的圓角,下面兩個角依然是直角,如圖效果 通過貝塞爾曲線重繪layer層 uiimageview picimageview uiimageview alloc initwithframe cgrectmake 100,100,100,100 picimagevie...

iOS任意圓角與View漸變

開發中經常不規則圓角及漸變的需求,看著挺複雜,其實實現只需要幾句 先看看效果圖,右上角的view既包含不規則的圓角,又包含漸變,裡面的按鈕也包含漸變 實現 self.totalview uiview alloc initwithframe cgrectmake kscreenwidth 140,20...

UIView設定部分角為圓角

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