`
zl4393753
  • 浏览: 333258 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

iphone 绘图 虚线的画法

阅读更多
虚线:
CGContextSetStrokeColorWithColor(myContext, [UIColor blackColor].CGColor);
CGContextSetLineDash (myContext,phase,lengths,2);
CGContextClosePath(myContext);
CGContextStrokePath(myContext);


切线:
An arc may be drawn by specifying two tangent points and a radius using the CGContextAddArcToPoint() function:
- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGContextMoveToPoint(context, 100, 100);
        CGContextAddArcToPoint(context, 100,200, 300,200, 100);
        CGContextStrokePath(context);
}





Drawing an Ellipse or Circle
Circles and ellipses are drawn by defining the rectangular area into which the shape must fit and then calling the CGContextAddEllipseInRect() function:
- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGRect rectangle = CGRectMake(60,170,200,80);

        CGContextAddEllipseInRect(context, rectangle);

        CGContextStrokePath(context);
}






Drawing a Cubic Bézier Curve
A cubic Bézier curve may be drawn by moving to a start point and then passing two control points and an end point through to the CGContextAddCurveToPoint() function:
- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGContextMoveToPoint(context, 10, 10);

        CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);

        CGContextStrokePath(context);
}






Drawing a Quadratic Bézier Curve
A quadratic Bézier curve is drawn using the CGContextAddQuadCurveToPoint() function, providing a control and end point as arguments having first moved to the start point:
- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGContextMoveToPoint(context, 10, 200);

        CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);

        CGContextStrokePath(context);
}





Dashed Line Drawing
So far in this chapter we have performed all our drawing with a solid line. Quartz also provides support for drawing dashed lines. This is achieved via the Quartz CGContextSetLineDash() function which takes as its arguments the following:
context – The graphics context of the view on which the drawing is to take place
phase - A floating point value that specifies how far into the dash pattern the line starts
lengths – An array containing values for the lengths of the painted and unpainted sections of the line. For example an array containing 5 and 6 would cycle through 5 painted unit spaces followed 6 unpainted unit spaces.
count – A count of the number of items in the lengths array
For example, a [2,6,4,2] lengths array applied to a curve drawing of line thickness 5.0 will appear as follows:
The corresponding drawRect code that drew the above line reads as follows:
- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 5.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGFloat dashArray[] = {2,6,4,2};

        CGContextSetLineDash(context, 3, dashArray, 4);

        CGContextMoveToPoint(context, 10, 200);

        CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);

        CGContextStrokePath(context);
}





















  • 大小: 12.7 KB
  • 大小: 17 KB
  • 大小: 17.1 KB
  • 大小: 16.2 KB
  • 大小: 15 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics