iOS日?;A(chǔ)記錄13




- (void)drawLine {
    //1.獲取當(dāng)前跟View相關(guān)聯(lián)的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路徑
    UIBezierPath *path = [UIBezierPath bezierPath];
    //一個路徑可以描述多條線/
    //2.1設(shè)置起點
    //坐標原點是以當(dāng)前繪制View的左上角為(0,0)
    [path moveToPoint:CGPointMake(50, 50)];
    //2.2添加一根線到某個點
    [path addLineToPoint:CGPointMake(150, 150)];
    
    [path moveToPoint:CGPointMake(100, 50)];
    [path addLineToPoint:CGPointMake(150, 250)];
    
    //把上一路徑的終點做為下一個路徑的起點
    [path addLineToPoint:CGPointMake(250, 50)];
    
    //設(shè)置上下文的狀態(tài)
    //設(shè)置線寬度
    CGContextSetLineWidth(ctx, 10);
    //設(shè)置上下文的連接樣式
    CGContextSetLineJoin(ctx, kCGLineJoinBevel);
    //設(shè)置頂角樣式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    //設(shè)置線的顏色
    //setStroke setFill
    //如果直接使用set,會自動匹配渲染的模式.
    [[UIColor redColor] set];
    
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    
    //4.把上下文的當(dāng)中繪制的所有路徑渲染View相關(guān)聯(lián)的layer當(dāng)中.
    //渲染的方式有兩種:
    //描邊:stroke
    //填充:fill
    CGContextStrokePath(ctx);
}
- (void)quadCurveLine {
    //1.獲取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路徑
    UIBezierPath *path = [UIBezierPath bezierPath];
    //畫曲線
    [path moveToPoint:CGPointMake(50, 250)];
    //添加一根曲線到某個點
    [path addQuadCurveToPoint:CGPointMake(250, 250) controlPoint:CGPointMake(50, 50)];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.把上下文的內(nèi)容渲染到View的layer;
    CGContextStrokePath(ctx);
}
- (void)drawShape {
    //在此方法內(nèi)部會自動創(chuàng)建 一個跟View相關(guān)聯(lián)的上下文
    //可以直接獲取
    //無論是開啟上下文,還是獲取上下文,都是以UIGraphics
    
    //1.獲取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路徑
    //UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 100)];
    //描述圓角矩形
    //cornerRadius:圓角半徑
    //UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 200, 200) cornerRadius:100];
    
    //描述橢圓
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 200)];
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    
    //設(shè)置上下文的狀態(tài),必須得要在渲染之前設(shè)置.
    [[UIColor yellowColor] set];
    
    //4.把上下文的內(nèi)容渲染到View的layer;
    //CGContextStrokePath(ctx);
    //填充
    CGContextFillPath(ctx);
    
    //畫橢圓
    //  只要是往Veiw上面畫東西,必須得要在drawRect當(dāng)中進行
    //UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 200)];
    
    //[[UIColor redColor] set];
    //[path stroke];
}
平時我們設(shè)置的圓角半徑,就是圖中兩點之間的距離
//作用:專門用來繪圖
//什么時候調(diào)用:當(dāng)View顯示時調(diào)用
//參數(shù):當(dāng)前View的bounds
- (void)drawRect:(CGRect)rect {
    //畫弧
    //Center:弧所在圓的圓心
    //radius:弧所在圓的半徑
    //startAngle:開始角度 ;0度是在圓的最右側(cè),向上,度數(shù)是負的,向下,度數(shù)是正的.
    //endAngle:截至角度  (哪個位置)
    //clockwise:是否為順時針 (怎么樣到這個位置)
    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    CGFloat radius = rect.size.width * 0.5 - 10;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center 
    radius:radius startAngle:0 endAngle:-M_PI_2 clockwise:NO];
    
    //添加一根線到圓心
    [path addLineToPoint:center];
    //關(guān)閉路徑(從路徑的終點連接一根到到路徑的起點)
    //[path closePath];
    
    [path fill]; //當(dāng)使用填充時,會自動關(guān)閉路徑
    //1.獲取上下文->2.描述路徑->3.把路徑添加到上下文->4.把上下文的內(nèi)容渲染到View的layer;
}
繪制下載進度條
#import "ProgressView.h"

@implementation ProgressView

- (void)setProgressValue:(CGFloat)progressValue {
    _progressValue = progressValue;
    //[self drawRect:self.bounds];
    //當(dāng)系統(tǒng)調(diào)用drawRect方法時 在drawRect:內(nèi)部會自動創(chuàng)建跟View相關(guān)聯(lián)的上下文
    //手動調(diào)用,是不會創(chuàng)建上下文.
    
    //通知系統(tǒng),調(diào)用drawRect
    //重繪,就是調(diào)用drawRect,重新繪制
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    CGFloat radius = rect.size.width * 0.5 - 10;
    CGFloat startA = -M_PI_2;
    CGFloat endA = startA + self.progressValue * M_PI * 2;
    
    //1.獲取跟View相關(guān)聯(lián)的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.描述路徑
      UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    
    [path addLineToPoint:center];
    
    //3.把路徑添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    
    [[UIColor colorWithWhite:1 alpha:0.5] set];
    
    //4.把上下文的內(nèi)容渲染的View上
    CGContextFillPath(ctx);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容