美文网首页
UIBezierPath ios版简单应用

UIBezierPath ios版简单应用

作者: 帅气的爽爽 | 来源:发表于2017-03-15 10:29 被阅读38次

    1、可以用来画遮罩。自定义圆角

    + (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];

    view2.backgroundColor = [UIColor whiteColor];

    view2.backgroundColor = [UIColor redColor];

    [self.view addSubview:view2];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(0, 100)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    maskLayer.frame = view2.bounds;

    maskLayer.path = maskPath.CGPath;

    view2.layer.mask = maskLayer;

    2、绘制图形

    (1)、drawRect方面里面直接构建路径然后    [aPath stroke]

    -(void)drawRect:(CGRect)rect{

    UIColor *color = [UIColor redColor];

    [color set];  //设置线条颜色

    UIColor *fillColor = [UIColor greenColor];

    [fillColor setFill];

    UIBezierPath* aPath = [UIBezierPath bezierPath];

    aPath.lineWidth = 5.0;

    aPath.lineCapStyle = kCGLineCapRound;  //线条拐角

    aPath.lineJoinStyle = kCGLineCapRound;  //终点处理

    // Set the starting point of the shape.

    [aPath moveToPoint:CGPointMake(100.0, 0.0)];

    // Draw the lines

    [aPath addLineToPoint:CGPointMake(200.0, 40.0)];

    [aPath addLineToPoint:CGPointMake(160, 140)];

    [aPath addLineToPoint:CGPointMake(40.0, 140)];

    [aPath addLineToPoint:CGPointMake(0.0, 40.0)];

    [aPath closePath]; //第五条线通过调用closePath方法得到的

    [aPath fill];

    [aPath stroke]; //Draws line 根据坐标点连线

    }

    (2)、添加在view的layer层

    UIBezierPath* aPath = [UIBezierPath bezierPath];

    aPath.lineWidth = 5.0;

    aPath.lineCapStyle = kCGLineCapRound;  //线条拐角

    aPath.lineJoinStyle = kCGLineCapRound;  //终点处理

    // Set the starting point of the shape.

    [aPath moveToPoint:CGPointMake(100.0, 0.0)];

    // Draw the lines

    [aPath addLineToPoint:CGPointMake(200.0, 40.0)];

    [aPath addLineToPoint:CGPointMake(160, 140)];

    [aPath addLineToPoint:CGPointMake(40.0, 140)];

    [aPath addLineToPoint:CGPointMake(0.0, 40.0)];

    aPath.lineWidth = 2;

    [aPath closePath]; //第五条线通过调用closePath方法得到的

    [aPath fill];

    [aPath stroke]; //Draws line 根据坐标点连线

    CAShapeLayer *layer = [[CAShapeLayer alloc]init];

    layer.path = aPath.CGPath;

    layer.fillColor = [UIColor greenColor].CGColor;

    layer.strokeColor = [UIColor redColor].CGColor;

    [self.view.layer addSublayer:layer];

    3、原生方法绘制图形

    //创建一个椭圆的贝塞尔曲线 半径相等 就是圆了

    UIBezierPath *mPath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(60, 60, 10, 10)];

    [mPath1 fill];

    //创建一个矩形的贝塞尔线

    UIBezierPath *mPath2 = [UIBezierPath bezierPathWithRect:CGRectMake(70,70, 10, 10)];

    [mPath2 stroke];

    //创建一个圆弧 传的弧度

    UIBezierPath *mPath3 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 100)

    radius:10

    startAngle:DEGREES_TO_RADIANS(0)

    endAngle:DEGREES_TO_RADIANS(180)

    clockwise:YES];

    [mPath3 fill];

    //创建一个 矩形的贝塞尔曲线, 带圆角

    UIBezierPath *mPath4 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(250, 15, 20, 20) cornerRadius:3];

    [mPath4 fill];

    //定义一个矩形 边角会变成 设置的角度  方位/角度大小

    UIBezierPath *mPath5 =[UIBezierPath bezierPathWithRoundedRect:CGRectMake(250, 45, 40, 40)

    byRoundingCorners:UIRectCornerTopLeft

    cornerRadii:CGSizeMake(10, 10)];

    [mPath5 fill];

    //定义一个二级的赛贝尔曲线 重点|拐弯点

    UIBezierPath *mPath6 = [UIBezierPath bezierPath];

    [mPath6 moveToPoint:CGPointMake(10,260)];

    [mPath6 addQuadCurveToPoint:CGPointMake(200,260) controlPoint:CGPointMake(85, 240)];

    [mPath6 setLineWidth:3];

    [mPath6 stroke];

    //定义一个三级的赛贝尔曲线 终点|拐点1|拐点2

    UIBezierPath *mPath7 = [UIBezierPath bezierPath];

    [mPath7 moveToPoint:CGPointMake(10,290)];

    [mPath7 addCurveToPoint:CGPointMake(300, 290)

    controlPoint1:CGPointMake(50, 270)

    controlPoint2:CGPointMake(140, 340)];

    [mPath7 stroke];

    相关文章

      网友评论

          本文标题:UIBezierPath ios版简单应用

          本文链接:https://www.haomeiwen.com/subject/kcclnttx.html