UIBezierPath基础使用

/**
 *  根据坐标画一个矩形
 *
 *  @param rect frame
 *
 *  @return UIBerzierPath路径
 */
+ (instancetype)bezierPathWithRect:(CGRect)rect;

UIBezierPath * path1 = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
    CAShapeLayer * layer1 = [[CAShapeLayer alloc]init];
    layer1.path = path1.CGPath;
    layer1.fillColor = [UIColor clearColor].CGColor;
    layer1.strokeColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:layer1];
/**
 *  根据矩形框画内切圆
 *
 *  @param rect 矩形的frame
 *
 *  @return UIBezierPath
 */
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100,100)];
    CAShapeLayer * layer = [[CAShapeLayer alloc]init];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:layer];
/**
 *  给矩形添加圆角
 *
 *  @param rect         矩形框
 *  @param cornerRadius 圆角半径
 *
 *  @return UIBezierPath
 */
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

UIBezierPath * path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) cornerRadius:20];
    CAShapeLayer * layer2 = [[CAShapeLayer alloc]init];
    layer2.path = path2.CGPath;
    layer2.fillColor = [UIColor clearColor].CGColor;
    layer2.strokeColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer2];
/**
 *  给矩形的某个角加上圆角
 *
 *  @param rect        矩形frame
 *  @param corners     枚举类型(那个角)
 *  @param cornerRadii 半径
 *
 *  @return UIBezierPath
 */
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

UIBezierPath * path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 300, 100, 100) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)];
    CAShapeLayer * layer3 = [[CAShapeLayer alloc]init];
    layer3.path = path3.CGPath;
    layer3.fillColor = [UIColor clearColor].CGColor;
    layer3.strokeColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer3];
//画多边形
    UIBezierPath * path4 = [[UIBezierPath alloc]init];
    [path4 moveToPoint:CGPointMake(100.0, 100.0)];
    
    [path4 addLineToPoint:CGPointMake(200.0, 40.0)];
    [path4 addLineToPoint:CGPointMake(160, 140)];
    [path4 addLineToPoint:CGPointMake(40, 140)];
    [path4 addLineToPoint:CGPointMake(0, 40)];
    [path4 closePath];
    CAShapeLayer * layer4 = [[CAShapeLayer alloc]init];
    layer4.path = path4.CGPath;
    layer4.fillColor = [UIColor clearColor].CGColor;
    layer4.strokeColor = [UIColor blueColor].CGColor;
    [self.view.layer addSublayer:layer4];
/**
 *  画二元曲线
 *
 *  @param endPoint     终点
 *  @param controlPoint 曲线的基准点
 */
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;

UIBezierPath * path5 = [UIBezierPath bezierPath];
    path5.lineWidth = 5;
    path5.lineCapStyle = kCGLineCapRound;//线条拐角
    path5.lineJoinStyle = kCGLineCapRound;//终点处理
    [path5 moveToPoint:CGPointMake(20, 100)];
    [path5 addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 200)];
    CAShapeLayer * layer5 = [[CAShapeLayer alloc]init];
    layer5.path = path5.CGPath;
    layer5.fillColor = [UIColor clearColor].CGColor;
    layer5.strokeColor = [UIColor greenColor].CGColor;
    [path5 stroke];
    [self.view.layer addSublayer:layer5];

/**
 *  以三个点画一段曲线,一般和moveToPoint配合使用
 *
 *  @param endPoint      曲线的终点
 *  @param controlPoint1 画曲线的第一个基准点
 *  @param controlPoint2 画曲线的第二个基准点
 */
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;

UIBezierPath * path6 = [UIBezierPath bezierPath];
    [path6 moveToPoint:CGPointMake(20, 50)];
    path6.lineWidth = 10;
    [path6 addCurveToPoint:CGPointMake(200, 50) controlPoint1:CGPointMake(110, 0) controlPoint2:CGPointMake(110, 100)];
    CAShapeLayer * layer6 = [[CAShapeLayer alloc]init];
    layer6.path = path6.CGPath;
    layer6.fillColor = [UIColor clearColor].CGColor;
    layer6.strokeColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:layer6];

你可能感兴趣的:(UIBezierPath基础使用)