UIBezierPath的使用

 使用UIBezierPath类可以创建基于矢量的路径.包括ARCs(圆弧),lines(直线),curves(曲线)。

下面我按照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];
}

运行结果如下。


是不是这个类很强大呢?




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值