iOS之CGPath的應用(二)


1、矩形路徑

CG_EXTERN CGPathRef  CGPathCreateWithRect(CGRect rect,
    const CGAffineTransform * __nullable transform)
- (void)createWithRect{
    CAShapeLayer *firstLayer = [CAShapeLayer layer];
    firstLayer.path = CGPathCreateWithRect(CGRectMake(90, 90, 20, 20), nil);
    firstLayer.fillColor = [UIColor redColor].CGColor;
    firstLayer.strokeColor = [UIColor blueColor].CGColor;
    firstLayer.lineWidth = 2;
    [self.showView.layer addSublayer:firstLayer];
    self.pathRect.text = @"路徑frame(90, 90, 20, 20)";
}ForeverGuard博客園

效果圖

2、橢圓路徑

CG_EXTERN CGPathRef  CGPathCreateWithEllipseInRect(CGRect rect,
    const CGAffineTransform * __nullable transform)
//CGPathCreateWithEllipseInRect
//以矩形四邊相切畫圓,矩形寬高相等就是園
- (void)createWithEllipseInRect{
    CAShapeLayer *firstLayer = [CAShapeLayer layer];
    firstLayer.path = CGPathCreateWithEllipseInRect(CGRectMake(70, 50, 60,100), nil);
    firstLayer.fillColor = [UIColor redColor].CGColor;
    firstLayer.strokeColor = [UIColor blueColor].CGColor;
    firstLayer.lineWidth = 2;
    [self.showView.layer addSublayer:firstLayer];
    self.pathRect.text = @"路徑frame(70, 50, 60,100)";
}

效果圖

3、圓角矩形

CG_EXTERN CGPathRef  CGPathCreateWithRoundedRect(CGRect rect,
    CGFloat cornerWidth, CGFloat cornerHeight,
    const CGAffineTransform * __nullable transform)
CG_EXTERN void CGPathAddRoundedRect(CGMutablePathRef cg_nullable path,
    const CGAffineTransform * __nullable transform, CGRect rect,
    CGFloat cornerWidth, CGFloat cornerHeight)
//CGPathCreateWithRoundedRect
- (void)createWithRoundedRect{
    CAShapeLayer *firstLayer = [CAShapeLayer layer];
    firstLayer.path = CGPathCreateWithRoundedRect(CGRectMake(10,50 , 180, 100), 10, 15, nil);
    firstLayer.fillColor = [UIColor redColor].CGColor;
    firstLayer.strokeColor = [UIColor blueColor].CGColor;
    firstLayer.lineWidth = 2;
    [self.showView.layer addSublayer:firstLayer];
    self.pathRect.text = @"路徑frame(10,50 , 180, 100)";
}
//CGPathAddRoundedRect
- (void)addRoundedRect{
    CAShapeLayer *firstLayer = [CAShapeLayer layer];
    CGMutablePathRef wavePath = CGPathCreateMutable();
    CGPathAddRoundedRect(wavePath, nil, CGRectMake(10,50 , 180, 100), 10,15);
    firstLayer.path = wavePath;
    firstLayer.fillColor = [UIColor redColor].CGColor;
    firstLayer.strokeColor = [UIColor blueColor].CGColor;
    firstLayer.lineWidth = 2;
    [self.showView.layer addSublayer:firstLayer];
    self.pathRect.text = @"路徑frame(10,50 , 180, 100)";
}

效果圖

4、虛線路徑

CG_EXTERN CGPathRef __nullable CGPathCreateCopyByDashingPath(
    CGPathRef cg_nullable path, const CGAffineTransform * __nullable transform,
    CGFloat phase, const CGFloat * __nullable lengths, size_t count)
//CGPathCreateCopyByDashingPath
-(void)createCopyByDashingPath{
    CAShapeLayer *firstLayer = [CAShapeLayer layer];
    CGMutablePathRef wavePath = CGPathCreateMutable();
    CGPathAddRoundedRect(wavePath, nil, CGRectMake(10,50 , 180, 100), 10,15);
    CGFloat floats[] = {2,4,2,4};//可以自行設置多組
    firstLayer.path = CGPathCreateCopyByDashingPath(wavePath, nil, 4, floats, 4);
    firstLayer.fillColor = [UIColor redColor].CGColor;
    firstLayer.strokeColor = [UIColor blueColor].CGColor;
    firstLayer.lineWidth = 10;
    [self.showView.layer addSublayer:firstLayer];
    self.pathRect.text = @"路徑frame(10,50 , 180, 100)";
}

效果圖

5、斜線

CG_EXTERN CGPathRef __nullable CGPathCreateCopyByStrokingPath(
    CGPathRef cg_nullable path, const CGAffineTransform * __nullable transform,
    CGFloat lineWidth, CGLineCap lineCap,
    CGLineJoin lineJoin, CGFloat miterLimit)
//CGPathCreateCopyByStrokingPath
//typedef CF_ENUM(int32_t, CGLineJoin) {
//    kCGLineJoinMiter,   鋒利
//    kCGLineJoinRound,   圓角
//    kCGLineJoinBevel    貝塞爾風格
//};
//
//typedef CF_ENUM(int32_t, CGLineCap) {
//    kCGLineCapButt,     線冒精確到點(默認)
//    kCGLineCapRound,    線冒為半徑為線寬一半的圓弧
//    kCGLineCapSquare    線冒尖銳的過渡
//};
- (void)createCopyByStrokingPath{
    CAShapeLayer *firstLayer = [CAShapeLayer layer];
    CGMutablePathRef wavePath = CGPathCreateMutable();
    CGPathMoveToPoint(wavePath, nil, 10,200*.5);
    CGPathAddLineToPoint(wavePath, nil, self.showView.frame.size.width-10 , 200*0.5);
    CGPathAddLineToPoint(wavePath, nil, self.showView.frame.size.width-10, 10);
    CGPathAddLineToPoint(wavePath, nil, 10, 190);
    firstLayer.path = CGPathCreateCopyByStrokingPath(wavePath, nil,5, kCGLineCapRound, kCGLineJoinRound, 2);
    firstLayer.fillColor = [UIColor redColor].CGColor;
    firstLayer.strokeColor = [UIColor blueColor].CGColor;
    [self.showView.layer addSublayer:firstLayer];
}

效果圖

6、其它划線

- (void)linePath{
    CAShapeLayer *firstLayer = [CAShapeLayer layer];
    CGMutablePathRef wavePath = CGPathCreateMutable();
//    確定路徑起點
    CGPathMoveToPoint(wavePath, nil, 0,100);
//    畫一條直線
    CGPathAddLineToPoint(wavePath, nil, 100 , 100);
//    添加一段二次貝塞爾曲線
    CGPathAddQuadCurveToPoint(wavePath, nil, 110, 110, 100, 120);
//    添加一段三次貝塞爾曲線
    CGPathAddCurveToPoint(wavePath, nil, 110, 130, 100, 140, 110, 150);
//    追加一個矩形
    CGPathAddRect(wavePath, nil, CGRectMake(0, 0, 50, 50));
//    追加一組矩形
    CGRect rects[] = {CGRectMake(60, 0, 50, 50),CGRectMake(120, 0, 50, 50)};
    CGPathAddRects(wavePath, nil, rects, 2);
//    追加一組線條
    CGPoint points[] = {CGPointMake(10, 80),CGPointMake(30, 80),CGPointMake(20, 90)};
    CGPathAddLines(wavePath, nil, points, 3);
//    追加一個橢圓
    CGPathAddEllipseInRect(wavePath, nil, CGRectMake(10, 150, 80, 50));
//    追加一段圓弧
    CGPathAddRelativeArc(wavePath, nil, 150, 175, 20, 0, M_PI_4);
//    追加一段圓弧
    CGPathAddArc(wavePath, nil, 130, 150, 10, 0, M_PI, NO);
//    追加一段相切弧
    CGPathAddArcToPoint(wavePath, nil, 130, 100, 160, 100, 80);
    firstLayer.path = wavePath;
    firstLayer.fillColor = [UIColor redColor].CGColor;
    firstLayer.strokeColor = [UIColor blueColor].CGColor;
    [self.showView.layer addSublayer:firstLayer];
}

效果圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM