有時候需要為uiview新增虛線描邊,本文記錄一種實現方式,主要是通過對uiview的根layer新增cashapelayer來完成。效果圖如下:
cgsize screensize = [uiscreen mainscreen].bounds.size;
cgfloat viewwidth = 200;cgfloat viewheight = 200;
uiview *view = [[uiview alloc] initwithframe:cgrectmake((screensize.width - viewwidth)/2, (screensize.height - viewheight) / 2, viewwidth, viewheight)];
view.backgroundcolor = [uicolor colorwithwhite:0.9 alpha:1];
view.layer.cornerradius = cgrectgetwidth(view.bounds)/2;
cashapelayer *borderlayer = [cashapelayer layer];
borderlayer.bounds = cgrectmake(0, 0, viewwidth, viewheight);
borderlayer.position = cgpointmake(cgrectgetmidx(view.bounds), cgrectgetmidy(view.bounds));//
borderlayer.path = [uibezierpath bezierpathwithrect:borderlayer.bounds].cgpath;
borderlayer.path = [uibezierpath bezierpathwithroundedrect:borderlayer.bounds cornerradius:cgrectgetwidth(borderlayer.bounds)/2].cgpath;borderlayer.linewidth = 1. / [[uiscreen mainscreen] scale];
//虛線邊框borderlayer.linedashpattern = @[@8, @8];
//實線邊框// borderlayer.linedashpattern = nil;
borderlayer.fillcolor = [uicolor clearcolor].cgcolor;
borderlayer.strokecolor = [uicolor redcolor].cgcolor;
[view.layer addsublayer:borderlayer];
[self.view addsubview:view];
可通過修改uibezierpath來改變虛線框的路徑。如果想把邊框繪製成實線,可將borderlayer.linedashpattern
置為nil
即可。
iOS為UIView設定陰影效果
uiview的陰影設定主要通過uiview的layer的相關屬性來設定 陰影的顏色 im iew.layer.shadowcolor uicolor bhxyhlxtbarlackcolor cgcolor 陰影的透明度 im iew.layer.shadowopacity 0.8f 陰影的圓角 i...
iOS給UIView新增常用的Extension
這個看個人習慣,不一定每個人的都一樣。最好自己寫乙個,以後就用自己的。import inte ce uiview setsize property nonatomic cgfloat framex x property nonatomic cgfloat framey y property nona...
iOS給UIView新增點選事件
我要給乙個uiview物件topview新增點選事件,方法如下 步驟1 建立手勢響應函式 1 void event uitapgesturerecognizer gesture 2 步驟2 建立手勢 1 uitapgesturerecognizer tapgesture uitapgesturere...