iOS CGPathAddArcToPoint 方法 画弧度

image-20210728221228171

要画出上图形状的视图,可以用CGPathAddArcToPoint 进行绘制(demo在文未)

如下图: A 为上次在上下文中添加的点

截屏2021-07-28 下午10.27.31
截屏2021-07-28 下午8.57.28

上图的demo:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CAShapeLayer *shapelayer = [[CAShapeLayer alloc]init];
    shapelayer.frame = CGRectMake(100, 200, 200, 200);
    
    // A点 = (xa,ya)
    CGFloat xa = 100;
    CGFloat ya = 100;
    CGPoint A = CGPointMake(xa, ya);
    // B点 = (xb,yb)
    CGFloat xb = 10;
    CGFloat yb = 100;
    CGPoint B = CGPointMake(xb, yb);
    // C点 = (xc,yc)
    CGFloat xc = 10;
    CGFloat yc = 10;
    CGPoint C = CGPointMake(xc, yc);
    
    CGMutablePathRef pathM = CGPathCreateMutable();
    CGPathMoveToPoint(pathM, &CGAffineTransformIdentity, A.x, A.y);
    CGPathAddArcToPoint(pathM, &CGAffineTransformIdentity, B.x, B.y, C.x, C.y, 10);
    CGPathAddLineToPoint(pathM, &CGAffineTransformIdentity, C.x, C.y);
    shapelayer.path = pathM;
    shapelayer.strokeColor = UIColor.redColor.CGColor;
    shapelayer.fillColor = [UIColor colorWithWhite:0 alpha:0.2].CGColor;
    shapelayer.lineWidth = 1;
    
    
    if (pathM) {
        CFRelease(pathM);
    }
    [self.view.layer addSublayer:shapelayer];
    
}

第一张图的demo

#import "PYDialogBoxView.h"
@interface PYDialogBoxView()
@property (nonatomic,assign) CGRect oldBoxRect;
@property (nonatomic,strong) CAShapeLayer *backgroundLayer;
@end

@implementation PYDialogBoxView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.layer addSublayer:self.backgroundLayer];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (CGRectEqualToRect(self.oldBoxRect, self.bounds)) {
        return;
    }
    self.oldBoxRect = self.bounds;
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.backgroundLayer.frame = self.bounds;
    [self layoutSelectedBgLayer];
    [CATransaction commit];
}

- (void) layoutSelectedBgLayer {
    CGFloat arrowBottomOffsetH = 6;
    CGFloat w = _backgroundLayer.frame.size.width;
    CGFloat h = _backgroundLayer.frame.size.height;

    CGFloat arrowTopY = h - arrowBottomOffsetH;
    CGFloat arrowW = 14;
    CGFloat arrowMidX = MAX(0,w/2.0f);
    CGFloat arrowLeftX = MAX(0, arrowMidX - arrowW/2.0f);
    CGFloat arrowRightX = MAX(0, arrowMidX + arrowW/2.0f);
    
    CGRect cutRect = CGRectMake(0, 0, w, h-arrowBottomOffsetH);
    CGFloat
    minx = CGRectGetMinX(cutRect),//矩形中最小的x
    midx = CGRectGetMidX(cutRect),//矩形中最大x值的一半
    maxx = CGRectGetMaxX(cutRect);//矩形中最大的x值
    
    CGFloat
    miny = CGRectGetMinY(cutRect),//矩形中最小的Y值
    midy = CGRectGetMidY(cutRect),//矩形中最大Y值的一半
    maxy = CGRectGetMaxY(cutRect);//矩形中最大的Y值
    
    CGFloat
    leftTopRadiu = 4,
    rightTopRadiu = 4,
    leftBottomRadiu = 4,
    rightBottomRadiu =  4;
    
    
    CGMutablePathRef path = CGPathCreateMutable();
    // 点A
    CGPathMoveToPoint(path, nil, minx, midy);
    
    /// CGPathAddArcToPoint
    /// 点A(minx,midY), B(minx,miny),C(midx,miny)
    /// 线l1 = (A->B)
    /// 线l2 = (B->C)
    /// 画线l1与线l2的内切圆
    CGPathAddArcToPoint(path, nil, minx, miny, midx, miny, leftTopRadiu);// 左上角
    CGPathAddArcToPoint(path, nil, maxx, miny, maxx, midy, rightTopRadiu);// 右上角
    CGPathAddArcToPoint(path, nil, maxx, maxy, midx, maxy, rightBottomRadiu);// 右下角
    
    // 三角形
    CGPathAddLineToPoint(path, nil, arrowRightX, arrowTopY);
    CGPathAddArcToPoint(path, nil, arrowMidX, h, arrowLeftX, arrowTopY, 2);// 右下角
    CGPathAddLineToPoint(path, &CGAffineTransformIdentity, arrowLeftX, arrowTopY);
    
    // 左下角
    CGPathAddArcToPoint(path, nil, minx, maxy, minx, midy, leftBottomRadiu);
    
    // 闭合
    CGPathCloseSubpath(path);
    
    self.backgroundLayer.path = path;
    
    if (path) {// 删除path
        CFRelease(path);
    }
}

- (CAShapeLayer *)backgroundLayer {
    if (!_backgroundLayer) {
        _backgroundLayer = [CAShapeLayer layer];
        
        _backgroundLayer.fillColor = UIColor.redColor.CGColor;
        _backgroundLayer.lineWidth = 1;
        _backgroundLayer.strokeColor = UIColor.blueColor.CGColor;
    }
    return _backgroundLayer;
}

@end


你可能感兴趣的:(iOS CGPathAddArcToPoint 方法 画弧度)