ios 繪製線框 iOS 各種邊框

2021-10-16 15:32:40 字數 3784 閱讀 5429

uiview *view = [[uiview alloc] initwithframe:(cgrectmake(0, 0, 100, 100))];

view.center = self.view.center;

[self.view addsubview:view];

view.backgroundcolor = [uicolor graycolor];

一、實線邊框

實線邊框.png

view.layer.bordercolor = [uicolor redcolor].cgcolor;

view.layer.borderwidth = 1;

二、虛線邊框

1.虛線邊框主要實現是通過增加乙個layer繪製乙個虛線的矩形,linedashpattern 第乙個引數代表線段長度,第二個引數代表線段間距。

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

dottedlineborder.frame = cgrectmake(0, 0, view.frame.size.width, view.frame.size.height);

[dottedlineborder setlinewidth:2];

[dottedlineborder setstrokecolor:[uicolor redcolor].cgcolor];

[dottedlineborder setfillcolor:[uicolor clearcolor].cgcolor];

dottedlineborder.linedashpattern = @[@10,@20];//10 - 線段長度 ,20 - 線段與線段間距

uibezierpath *path = [uibezierpath bezierpathwithrect:dottedlineborder.frame];

dottedlineborder.path = path.cgpath;

[view.layer addsublayer:dottedlineborder];

虛線.png

2.流動的虛線

有一些裁剪時會顯示乙個矩形虛線,而虛線還在流動。

這裡通過改變線的長度來實現虛線的流動,如果要求不嚴謹,只要粗略的實現這樣實現:

__block nsnumber * i = @10;

[nstimer scheduledtimerwithtimeinterval:0.5 repeats:yes block:^(nstimer * _nonnull timer) {

i = [nsnumber numberwithint:([i intvalue]+1)];

dottedlineborder.linedashpattern = @[i,@10];

if ([i intvalue]>=20) {

i = [nsnumber numberwithint:10];

三內邊距邊框

內邊距邊框.png

cgrect viewrect = view.frame;

cgfloat bordermargin = 20;//內邊距

cgsize borderoffset = cgsizemake(0, 0);//邊框x,y偏移量

uiview *lineview = [[uiview alloc]initwithframe:cgrectmake(viewrect.origin.x+borderoffset.width, viewrect.origin.y+borderoffset.height, viewrect.size.width, viewrect.size.height)];

[self.view addsubview:lineview];

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

dottedlineborder.frame = cgrectmake(-bordermargin/2, -bordermargin/2, view.frame.size.width+bordermargin*2, view.frame.size.height+bordermargin*2);

[dottedlineborder setlinewidth:2];

[dottedlineborder setstrokecolor:[uicolor redcolor].cgcolor];

[dottedlineborder setfillcolor:[uicolor clearcolor].cgcolor];

// dottedlineborder.linedashpattern = @[@10,@20];//10 - 線段長度 ,20 - 線段與線段間距

//矩形

uibezierpath *path = [uibezierpath bezierpathwithrect:dottedlineborder.frame];

dottedlineborder.path = path.cgpath;

[lineview.layer addsublayer:dottedlineborder];

[self.view sendsubviewtoback:lineview];

四、偏移的邊框

偏移邊框.png

設定borderoffset即可指定邊框的xy偏移量

五、指定某角為圓角的邊框

指定某角為圓角的邊框.png

uibezierpath *path = [uibezierpath bezierpathwithroundedrect:dottedlineborder.frame byroundingcorners:uirectcornertopleft|uirectcornertopright cornerradii:(cgsizemake(10, 10))];

六、圓形內鏤空的邊框

圓形鏤空邊框.png

/*圓形鏤空邊框*/

uibezierpath *path = [uibezierpath bezierpathwithovalinrect:dottedlineborder.frame];

uibezierpath *circlepath = [uibezierpath bezierpathwithrect:cgrectmake(bordermargin/2, bordermargin/2, lineview.frame.size.width, lineview.frame.size.height)];

[path setusesevenoddfillrule:yes];

dottedlineborder.fillrule = kcafillruleevenodd;

dottedlineborder.fillcolor = [uicolor redcolor].cgcolor;

七、詭異的邊框

詭異的邊框.png

uibezierpath *path = [uibezierpath bezierpathwithovalinrect:dottedlineborder.frame];

uibezierpath *circlepath = [uibezierpath bezierpathwithrect:(cgrectmake(0, 0, view.frame.size.width+bordermargin, view.frame.size.height+bordermargin))];

[path setusesevenoddfillrule:yes];

dottedlineborder.fillrule = kcafillruleevenodd;

dottedlineborder.fillcolor = [uicolor redcolor].cgcolor;

ios 繪製線框 iOS中畫矩形的幾種方法總結

方法1 pragma mark 畫矩形方法1 void drawrect1 1取得圖形上下文 cgcontextref ctx uigraphicsgetcurrentcontext 2畫一條線段 設定乙個起點 cgcontextmovetopoint ctx,20,20 cgcontextaddl...

ios 繪製線條

新增被塞爾曲線 cgcontextmovetopoint cgcontext,150,100 移動到某乙個點 cgcontextaddcurvetopoint cgcontext,150,100,100,100,100,150 繪製被塞爾曲線,點伴隨移動,這三個引數呢,每2個為乙個引數 1.poin...

IOS各種動畫

1。html view plain copy uibezierpath movepath uibezierpath bezierpath movepath movetopoint center movepath addquadcurvetopoint topos controlpoint cgpoi...