iPhone繪圖關(guān)于QuartZ中繪制Line案例
iPhone繪圖關(guān)于QuartZ中繪制Line案例是本文要介紹的內(nèi)容,主要介紹了如何在QuartZ中繪制Line的內(nèi)容,來(lái)看詳細(xì)內(nèi)容。下面的代碼和例子都是從官方的Quartz Demo中截取的,在此在寫下以便以后用到。
1.基本的劃線代碼。
- CGContextRef context = UIGraphicsGetCurrentContext();
- // Drawing lines with a white stroke color
- CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
- // Draw them with a 2.0 stroke width so they are a bit more visible.
- CGContextSetLineWidth(context, 2.0);
- // Draw a single line from left to right
- CGContextMoveToPoint(context, 10.0, 30.0);
- CGContextAddLineToPoint(context, 310.0, 30.0);
- CGContextStrokePath(context);
- // Draw a connected sequence of line segments
- CGPoint addLines[] =
- {
- CGPointMake(10.0, 90.0),
- CGPointMake(70.0, 60.0),
- CGPointMake(130.0, 90.0),
- CGPointMake(190.0, 60.0),
- CGPointMake(250.0, 90.0),
- CGPointMake(310.0, 60.0),
- };
- // Bulk call to add lines to the current path.
- // Equivalent to MoveToPoint(points[0]); for(i=1; i<count; ++i) AddLineToPoint(points[i]);
- CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
- CGContextStrokePath(context);
- // Draw a series of line segments. Each pair of points is a segment
- CGPoint strokeSegments[] =
- {
- CGPointMake(10.0, 150.0),
- CGPointMake(70.0, 120.0),
- CGPointMake(130.0, 150.0),
- CGPointMake(190.0, 120.0),
- CGPointMake(250.0, 150.0),
- CGPointMake(310.0, 120.0),
- };
- // Bulk call to stroke a sequence of line segments.
- // Equivalent to for(i=0; i<count; i+=2) { MoveToPoint(point[i]); AddLineToPoint(point[i+1]); StrokePath(); }
- CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0]));
效果如下圖:
2.畫(huà)虛線
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
- // Each dash entry is a run-length in the current coordinate system
- // The concept is first you determine how many points in the current system you need to fill.
- // Then you start consuming that many pixels in the dash pattern for each element of the pattern.
- // So for example, if you have a dash pattern of {10, 10}, then you will draw 10 points, then skip 10 points, and repeat.
- // As another example if your dash pattern is {10, 20, 30}, then you draw 10 points, skip 20 points, draw 30 points,
- // skip 10 points, draw 20 points, skip 30 points, and repeat.
- // The dash phase factors into this by stating how many points into the dash pattern to skip.
- // So given a dash pattern of {10, 10} with a phase of 5, you would draw 5 points (since phase plus 5 yields 10 points),
- // then skip 10, draw 10, skip 10, draw 10, etc.
- CGContextSetLineDash(context, dashPhase, dashPattern, dashCount);
- // Draw a horizontal line, vertical line, rectangle and circle for comparison
- CGContextMoveToPoint(context, 10.0, 20.0);
- CGContextAddLineToPoint(context, 310.0, 20.0);
- CGContextMoveToPoint(context, 160.0, 30.0);
- CGContextAddLineToPoint(context, 160.0, 130.0);
- CGContextAddRect(context, CGRectMake(10.0, 30.0, 100.0, 100.0));
- CGContextAddEllipseInRect(context, CGRectMake(210.0, 30.0, 100.0, 100.0));
- // And width 2.0 so they are a bit more visible
- CGContextSetLineWidth(context, 2.0);
- CGContextStrokePath(context);
其中CGFloat lengths[]是一個(gè)CGFloat類型的數(shù)組,dashCount表示數(shù)組元素的個(gè)數(shù)。下面將給出5種情況下虛線的圖片:
{{10.0, 10.0}, 2}如下圖,先默認(rèn)dashPhase為0.
dash pattern為{10,10}表示的是先繪制10 points,然后跳過(guò)10 points,然后重復(fù),就向上圖。
{{10.0, 20.0, 10.0}, 3}如下圖:
這是一個(gè)奇數(shù)個(gè)的例子,就是先繪制10 points, 接著跳過(guò)20 points,再繪制10points,接著跳過(guò)10 points,再繪制20points,在跳過(guò)10 points,然后接著重復(fù)。
{{10.0, 20.0, 30.0}, 3},如下圖
{{10.0, 20.0, 10.0, 30.0}, 4},如下圖
{{10.0, 10.0, 20.0, 30.0, 50.0}, 5},如下圖:
函數(shù)CGContextSetLineDash的函數(shù)dashPhase參數(shù)表示虛線在繪制的時(shí)候跳過(guò)多少個(gè)points。舉一個(gè)例子,dash pattern為{10,10},dashPhase為5,則我們繪制5 points,接著跳過(guò)10 points,繪制10, 跳過(guò)10 ,繪制10 。。。重復(fù)。
小結(jié):iPhone繪圖關(guān)于QuartZ中繪制Line案例的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!如果想深入了解iphone繪圖的更多內(nèi)容,請(qǐng)參考: