UIBezierPath - 贝塞尔曲线

一般只能在drawRet中绘制。但是结合 CAShapeLayer,绘制到layer上在add到View上就能随机绘制了。这里只讲UIBezierPath。CAShapeLayer 很简单,看另外一篇好了。

指定图形

    UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 400, 100)];// 矩形
    UIBezierPath *path2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(90, 90, 120, 120)];// 内接圆
    UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(90, 90, 120, 120) cornerRadius:12];// 圆角矩形
    UIBezierPath *path4 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(90, 90, 120, 120) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 2)];// 选择性圆角矩形
    /*
     typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
     UIRectCornerTopLeft     = 1 << 0,
     UIRectCornerTopRight    = 1 << 1,
     UIRectCornerBottomLeft  = 1 << 2,
     UIRectCornerBottomRight = 1 << 3,
     UIRectCornerAllCorners  = ~0UL
     };// 哪个角
     */
    
    UIBezierPath *path5 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(120, 120) radius:120 startAngle:0 endAngle:M_PI/2 clockwise:NO];// 弧线。(0 - 正x轴,2M_PI 是一圈。clockwise :是顺逆时针)
    

// 注意 ,只有图形是不够滴,还需要对图形添加属性,以及绘制,下面讲。

自定义 图形

    // 初始化
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(100, 100)];// 起点
    
    [path addLineToPoint:CGPointMake(120,120)];// 直线终点
    [path addCurveToPoint:CGPointMake(100, 200) controlPoint1:CGPointMake(120, 220) controlPoint2:CGPointMake(100, 300)];// 2次曲线
    [path addQuadCurveToPoint:CGPointMake(100, 200) controlPoint:CGPointMake(140, 230)];// 1次曲线
    [path addArcWithCenter:CGPointMake(200, 200) radius:80 startAngle:0 endAngle:M_PI clockwise:YES];// 圆弧
    
    [path closePath];// 首位相连

    [path appendPath:path2];// 拼接路径
    path = [path bezierPathByReversingPath];// 路径方向相反,图形是一样的
    
    [path removeAllPoints];// 删除所有路径

    // 3D变化,需要好好研究0.0
    [path applyTransform:CGAffineTransform];
   

路径属性 与 绘制

  • 基本绘制
     path.lineWidth = 1.;// 线宽
    
    // 颜色 填充色,描边色
    [[UIColor redColor] setFill];
    [[UIColor greenColor] setStroke];
    
    // 绘制
    [path fill];
    [path stroke];
    
    // 裁剪,作用不明
    [path addClip];
    
  • 更多好玩的属性
    path.miterLimit = 2.;// 防止转折处尖叫过长
    path.flatness = 2.;// 根据弯曲程度,决定渲染精度?
    path.usesEvenOddFillRule = NO;// 这个牛逼了,图形复杂是填充颜色的一种规则。类似棋盘。
    path.lineCapStyle = kCGLineCapRound;// 两端
    /*
     typedef CF_ENUM(int32_t, CGLineCap) {
     kCGLineCapButt,
     kCGLineCapRound,
     kCGLineCapSquare
     };
     */
    
    path.lineJoinStyle = kCGLineJoinRound;// 转折
    /*
     typedef CF_ENUM(int32_t, CGLineJoin) {
     kCGLineJoinMiter,
     kCGLineJoinRound,
     kCGLineJoinBevel
     };
     */

获取 属性

@property(nonatomic) CGPathRef CGPath;
- (CGPathRef)CGPath NS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;

@property(readonly,getter=isEmpty) BOOL empty;
@property(nonatomic,readonly) CGRect bounds;
@property(nonatomic,readonly) CGPoint currentPoint;
- (BOOL)containsPoint:(CGPoint)point;

其他

  • addClip:作用不明
  • (void)applyTransform:(CGAffineTransform)transform:水太深。
    1

你可能感兴趣的:(UIBezierPath - 贝塞尔曲线)