下面我按照iOS programming 上的demo做的例子。
CGRect bounds = self.bounds;
CGPoint center;
center.x = bounds.origin.x +bounds.size.width/2.0;
center.y = bounds.origin.y +bounds.size.height/2.0;
// // the circle will be the largest that will fit the view
// float radius = (MIN(bounds.size.width, bounds.size.height)/2.0);
//the largest circle will circumscribe the view
float maxRadius = hypot(bounds.size.width, bounds.size.height)/2.0;
<strong>UIBezierPath *path = [[UIBezierPath alloc] init];</strong>
// //add an arc to the path at center,with ridius of radius from 0 to 2*PI radians (a circle)
// [path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
for (float currentRadius = maxRadius; currentRadius>0; currentRadius -= 20) {
[path moveToPoint:CGPointMake(center.x +currentRadius , center.y)];
<strong> [path addArcWithCenter:center radius:currentRadius startAngle:0 endAngle:M_PI*2 clockwise:YES];</strong>
}
//configure line width to 10 points
path.lineWidth =10;
//Configure the drawing color to light gray
[[UIColor lightGrayColor]setStroke];
[path stroke];
运行结果图。
其次,我看了其他人的demo,做的笑脸。
- (void)drawRect:(CGRect)rect
{
[[UIColor blueColor] set];
[self drawHead];
[self drawEyes];
[self drawSmile];
}
- (void)drawHead
{
UIBezierPath *pathHead = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(40, 120, 240, 240)];
[pathHead setLineWidth:5];
[pathHead stroke];
}
-(void)drawEyes
{
UIBezierPath *pathEyes = [UIBezierPath bezierPath];
[pathEyes addArcWithCenter:CGPointMake(115, 190) radius:10 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
[pathEyes moveToPoint:CGPointMake(215, 190)];
[pathEyes addArcWithCenter:CGPointMake(205, 190) radius:10 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
[pathEyes setLineWidth:5];
[pathEyes stroke];
}
- (void)drawSmile
{
UIBezierPath *pathSmile = [UIBezierPath bezierPath];
//start
[pathSmile moveToPoint:CGPointMake(100, 280)];
[pathSmile addCurveToPoint:CGPointMake(220, 280) controlPoint1:CGPointMake(130, 330) controlPoint2:CGPointMake(190, 330)];
[pathSmile setLineWidth:5];
[pathSmile stroke];
}
运行结果如下。
是不是这个类很强大呢?