iOS学习

贝塞尔曲线-UIBezierPath

2016-12-13  本文已影响16人  buzaixian程序员
-(void)drawRect:(CGRect)rect
{
    [self drawLine1];
    [self drawLine2];
    [self drawRound];
    [self drawArc1];
    [self drawArc2];
    [self drawArc3];
}

/**
 画五边形
 */
-(void)drawLine1
{
    UIColor *color = [UIColor redColor];
    [color set];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    bezierPath.lineWidth = 2;
    bezierPath.lineCapStyle = kCGLineCapRound;
    bezierPath.lineJoinStyle = kCGLineJoinMiter;
    // 起点
    [bezierPath moveToPoint:CGPointMake(100.0, 50.0)];
    
    // 绘制线条
    [bezierPath addLineToPoint:CGPointMake(170.0, 70.0)];
    [bezierPath addLineToPoint:CGPointMake(140, 130)];
    [bezierPath addLineToPoint:CGPointMake(40.0, 130)];
    [bezierPath addLineToPoint:CGPointMake(20.0, 70.0)];
    
    [bezierPath closePath];  //最后一点和第一个点相连
    
    [bezierPath stroke];  //划线
//    [bezierPath fill];  //填充满
}

/**
 画四边形:bezierPath.frame画出来的四边形就有多大 =。=
 */
-(void)drawLine2
{
    UIColor *color = [UIColor yellowColor];
    [color set];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(250, 50, 100, 100)];
    [bezierPath closePath];
    [bezierPath stroke];
}

/**
 画圆(椭)形:bezierPath.frame画出来的圆形就有多大 =。=
 */
-(void)drawRound
{
    UIColor *color = [UIColor blueColor];
    [color set];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 200, 100, 50)]; // 宽和高相同则是圆形
    [bezierPath stroke];
}

/**
 画弧度
 */
-(void)drawArc1
{
    UIColor *color = [UIColor redColor];
    [color set];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(280, 230) radius:50 startAngle:0 endAngle:M_PI clockwise:YES];
    [bezierPath stroke];
}

/**
 画不规则的弧度
 */
-(void)drawArc2
{
    UIColor *color = [UIColor blueColor];
    [color set];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    //起点
    [bezierPath moveToPoint:CGPointMake(50, 300)];  
    //QuadCurveToPoint:终点  controlPoint:中途的那个点
    [bezierPath addQuadCurveToPoint:CGPointMake(150, 300) controlPoint:CGPointMake(50, 400)]; 
    [bezierPath stroke];
}
/**
 画不规则的弧度
 */
-(void)drawArc3
{
    UIColor *color = [UIColor blueColor];
    [color set];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    //起点
    [bezierPath moveToPoint:CGPointMake(200, 350)];  
    //QuadCurveToPoint:终点  controlPoint:中途的那个点
    [bezierPath  addCurveToPoint:CGPointMake(350, 350) controlPoint1:CGPointMake(270, 270) controlPoint2:CGPointMake(300, 400)];  
    [bezierPath stroke];
}

效果图如下

58FE8D0D-AAF2-43BC-B883-9E343C788D18.png
上一篇下一篇

猜你喜欢

热点阅读