iOS绘图二:UIBezierPath绘图

3 篇文章 0 订阅

整理下绘图

 CoreGraphics 绘图:iOS 绘图一,Layer绘制不再介绍详见:iOS 绘图三

UIBezierPath绘图:

- (void)drawRect:(CGRect)rect {
   
    self.layer.backgroundColor = [UIColor greenColor].CGColor;
    
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGFloat lineWidth = 3;
    
    [[UIColor redColor]setStroke];    //渲染颜色,外围线条色
    [[UIColor purpleColor]setFill];  //填充颜色,实心色
    
         //画矩形
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(lineWidth, lineWidth, width-2*lineWidth, height-2*lineWidth)];
    [path setLineWidth:lineWidth];  //线宽
    [path stroke];
    //填充。 可以和fill同时使用
//    [path fill];      //绘制圆外圈  可以和stroke同时使用
    //画圆角矩形
    UIBezierPath * roundedRecpath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 200, 100, 100) cornerRadius:20];
    [roundedRecpath fill];
    [roundedRecpath stroke];
    
    //部分角变圆
    /*
     UIRectCornerTopLeft    左上角
     UIRectCornerTopRight     右上角
     UIRectCornerBottomLeft  左下角
     UIRectCornerBottomRight 右下角
     UIRectCornerAllCorners  所有角
     */
    
    //角度以cornerRadii 的宽为准
    UIBezierPath * correctanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 200, 50, 50) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft cornerRadii:CGSizeMake(25, 0)];
    [correctanglePath fill];
    [correctanglePath stroke];
    
    //画圆
    UIBezierPath * circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(60, 60) radius:40 startAngle:0 endAngle:2*M_PI clockwise:0];
    
    circlePath.lineWidth = lineWidth;    //path 宽度。每个path需要单独设置线宽,想要共用,可以将这个path添加到已有的path里面 这个在后面介绍
    [path appendPath:circlePath];
    //填充圆(实心圆)。 可以和stroke同时使用
    [circlePath fill];
    //绘制圆外圈(空心圆)  可以和fill同时使用
    [circlePath stroke];

    //绘制圆弧
    UIBezierPath * circularArcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:40 startAngle:0 endAngle:M_PI/2 clockwise:1];
    [circularArcPath fill];
//    [circularArcPath stroke];

    //绘制直线
    UIBezierPath * linePath =  [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(20, 100)];
    [linePath addLineToPoint:CGPointMake(20, 160)];
    [linePath addLineToPoint:CGPointMake(100, 160)];
    [linePath setLineWidth:lineWidth];
    
    /*
     拐点样式设置
     kCGLineJoinMiter,//正常样式,默认
     kCGLineJoinRound,//圆角
     kCGLineJoinBevel //和Miter长一点,距离为线宽的1/2。
     */
    [linePath setLineJoinStyle:kCGLineJoinRound];
    /*
     端点样式设置
     kCGLineCapButt,//正常样式,默认
     kCGLineCapRound,//圆角
     kCGLineCapSquare //切脚
     */
    [linePath setLineCapStyle:kCGLineCapRound];
    
    [linePath stroke];
    
    //绘制三角形
    UIBezierPath * trianglePath =  [UIBezierPath bezierPath];
    [trianglePath moveToPoint:CGPointMake(100, 100)];
    [trianglePath addLineToPoint:CGPointMake(140, 100)];
    [trianglePath addLineToPoint:CGPointMake(100, 140)];
    [trianglePath closePath];
    [trianglePath fill];
//    [trianglePath stroke];

    
    //椭圆
    UIBezierPath * ellipsePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(200, 100, 100, 50)];
    [ellipsePath fill];
    [ellipsePath stroke];
    
    //剪切
//    UIBezierPath * showPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:50 startAngle:0 endAngle:2*M_PI clockwise:0];
//
//   UIBezierPath * clipPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(250, 200) radius:50 startAngle:0 endAngle:2*M_PI clockwise:0];
//
//    [clipPath addClip];    //有这句代码,整个drawrect里面绘制的都会被裁切cgcontext也会被裁切
    //    [showPath fill];
//
    
    //增加
    UIBezierPath * blowPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:50 startAngle:0 endAngle:2*M_PI clockwise:0];
    UIBezierPath * topPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(220, 150) radius:50 startAngle:0 endAngle:2*M_PI clockwise:0];
    [blowPath appendPath:topPath];
    
    [blowPath fill];

    
    //填充颜色和底色混合
    
    //自行翻译,和美工联系效果
    //    kCGBlendModeNormal,
    //    kCGBlendModeMultiply,
    //    kCGBlendModeScreen,
    //    kCGBlendModeOverlay,
    //    kCGBlendModeDarken,
    //    kCGBlendModeLighten,
    //    kCGBlendModeColorDodge,
    //    kCGBlendModeColorBurn,
    //    kCGBlendModeSoftLight,
    //    kCGBlendModeHardLight,
    //    kCGBlendModeDifference,
    //    kCGBlendModeExclusion,
    //    kCGBlendModeHue,
    //    kCGBlendModeSaturation,
    //    kCGBlendModeColor,
    //    kCGBlendModeLuminosity,
    //
    //s代表原色,Da代表原色的透明度。
    //    kCGBlendModeClear,                  /* R = 0 */
    //    kCGBlendModeCopy,                   /* R = S */
    //    kCGBlendModeSourceIn,               /* R = S*Da */
    //    kCGBlendModeSourceOut,              /* R = S*(1 - Da) */
    //    kCGBlendModeSourceAtop,             /* R = S*Da + D*(1 - Sa) */
    //    kCGBlendModeDestinationOver,        /* R = S*(1 - Da) + D */
    //    kCGBlendModeDestinationIn,          /* R = D*Sa */
    //    kCGBlendModeDestinationOut,         /* R = D*(1 - Sa) */
    //    kCGBlendModeDestinationAtop,        /* R = S*(1 - Da) + D*Sa */
    //    kCGBlendModeXOR,                    /* R = S*(1 - Da) + D*(1 - Sa) */
    //    kCGBlendModePlusDarker,             /* R = MAX(0, (1 - D) + (1 - S)) */
    //    kCGBlendModePlusLighter             /* R = MIN(1, S + D) */
    
    //在fill之前调用
//    [blowPath fillWithBlendMode:kCGBlendModeDifference alpha:0.2];
    
    //二次曲线
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(100, 200)];
    [bezierPath addQuadCurveToPoint:CGPointMake(300, 200) controlPoint:CGPointMake(250, 100)];
    [bezierPath stroke];
    
    // 三次曲线需要瞄点,
    UIBezierPath *threeBezierPath = [UIBezierPath bezierPath];
    [threeBezierPath moveToPoint:CGPointMake(100, 200)];
    [threeBezierPath addCurveToPoint:CGPointMake(300, 200) controlPoint1:CGPointMake(200, 100) controlPoint2:CGPointMake(200, 300)];
    [threeBezierPath stroke];
    
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值