iOS 繪製虛線的三種方法

2021-08-27 11:26:36 字數 2113 閱讀 8973

總結了三種繪製虛線的方法:

- (void)drawrect:(cgrect)rect ;

// 虛線的起始點

cgcontextsetlinedash(context, 0, lengths,2);

// 繪製虛線的終點

cgcontextaddlinetopoint(context, 310.0,20.0);

// 繪製

cgcontextstrokepath(context);

// 關閉影象

cgcontextclosepath(context);

}

/**

* 通過 quartz 2d 在 uiimageview 繪製虛線

* * param imageview 傳入要繪製成虛線的imageview

* return

*/- (uiimage *)drawlineofdashbyimageview:(uiimageview *)imageview ;

cgcontextsetstrokecolorwithcolor(line, [uicolor greencolor].cgcolor);

// 開始繪製虛線

cgcontextsetlinedash(line, 0, lengths, 2);

cgcontextmovetopoint(line, 0.0, 2.0);

cgcontextaddlinetopoint(line, 300, 2.0);

cgcontextstrokepath(line);

// uigraphicsgetimagefromcurrentimagecontext()返回的就是image

return uigraphicsgetimagefromcurrentimagecontext();

}

/**

* 通過 cashapelayer 方式繪製虛線

* * param lineview: 需要繪製成虛線的view

* param linelength: 虛線的寬度

* param linespacing: 虛線的間距

* param linecolor: 虛線的顏色

* param linedirection 虛線的方向 yes 為水平方向, no 為垂直方向

**/- (void)drawlineofdashbycashapelayer:(uiview *)lineview linelength:(int)linelength linespacing:(int)linespacing linecolor:(uicolor *)linecolor linedirection:(bool)ishorizonal else

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

// 設定虛線顏色為blackcolor

[shapelayer setstrokecolor:linecolor.cgcolor];

// 設定虛線寬度

if (ishorizonal) else

[shapelayer setlinejoin:kcalinejoinround];

// 設定線寬,線間距

[shapelayer setlinedashpattern:[nsarray arraywithobjects:[nsnumber numberwithint:linelength], [nsnumber numberwithint:linespacing], nil]];

// 設定路徑

cgmutablepathref path = cgpathcreatemutable();

cgpathmovetopoint(path, null, 0, 0);

if (ishorizonal) else

[shapelayer setpath:path];

cgpathrelease(path);

// 把繪製好的虛線新增上來

[lineview.layer addsublayer:shapelayer];

}

iOS 繪製虛線的三種方法

void drawrect cgrect rect 虛線的起始點 cgcontextsetlinedash context,0,lengths,2 繪製虛線的終點 cgcontextaddlinetopoint context,310.0 20.0 繪製 cgcontextstrokepath co...

iOS 繪製虛線的三種方法

void drawrect cgrect rect 虛線的起始點 cgcontextsetlinedash context,0,lengths,2 繪製虛線的終點 cgcontextaddlinetopoint context,310.0,20.0 繪製 cgcontextstrokepath co...

iOS撥打電話(三種方法)

2,這種方法,打完 後還會回到原來的程式,也會彈出提示,推薦這種 nsstring str nsstring alloc initwithformat tel 186 x6979 uiwebview callwebview uiwebview alloc init callwebview loadr...