iPhone繪圖關於QuartZ中繪製Line案例是本文要介紹的內容,主要介紹了如何在QuartZ中繪製Line的內容,來看詳細內容。下面的代碼和例子都是從官方的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.畫虛線
- 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[]是一個CGFloat類型的數組,dashCount表示數組元素的個數。下面將給出5種情況下虛線的圖片:
{{10.0, 10.0}, 2}如,先預設dashPhase為0.
dash pattern為{10,10}表示的是先繪製10 points,然後跳過10 points,然後重複,就向。
{{10.0, 20.0, 10.0}, 3}如:
這是一個奇數個的例子,就是先繪製10 points, 接著跳過20 points,再繪製10points,接著跳過10 points,再繪製20points,在跳過10 points,然後接著重複。
{{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},如:
函數CGContextSetLineDash的函數dashPhase參數表示虛線在繪製的時候跳過多少個points。舉一個例子,dash pattern為{10,10},dashPhase為5,則我們繪製5 points,接著跳過10 points,繪製10, 跳過10 ,繪製10 。。。重複。
小結:iPhone繪圖關於QuartZ中繪製Line案例的內容介紹完了,希望本文對你有所協助!如果想深入瞭解iphone繪圖的更多內容,請參考:
iPhone繪圖關於QuartZ中繪製Polygons案例
iPhone繪圖關於QuartZ中繪製Curves案例