貓貓學IOS(二十九)UI之Quartz2D自訂下載控制項,iosquartz2d

來源:互聯網
上載者:User

貓貓學IOS(二十九)UI之Quartz2D自訂下載控制項,iosquartz2d

貓貓分享,必須精品

素材代碼地址:http://blog.csdn.net/u013357243/article/details/45333795
原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents

效果

自訂控制項過程

主要過程在上一篇裡有介紹了,這裡主要介紹下代碼實現

先做好要放的view 然後實現呢主要就是四步:
1:擷取上下文
2:拼接路徑
3:把路徑添加到上下文。
4:把上下文渲染到視圖

// 1:擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2:拼接路徑    /*我們需要畫一個圓圖*/    CGPoint center = CGPointMake(50, 50);//圓心    CGFloat radius = 43;//半徑    CGFloat startA = -M_PI_2 ;//起始角度    CGFloat endA = -M_PI_2 + _progress * M_PI * 2 ;//結束角度。    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];    //clockwise 順時針方向。    //3:把路徑添加到上下文。    CGContextAddPath(ctx, path.CGPath);    //設定顏色為紅色    [[UIColor redColor] set];    //設定線條的寬度    CGContextSetLineWidth(ctx, 10);    //設定兩端的樣式為圓角    CGContextSetLineCap(ctx,kCGLineCapRound);    //4:把上下文渲染到視圖。    CGContextStrokePath(ctx);
Quartz2D要學習記住的地方畫直線

效果:

 // 當自訂view第一次顯示出來的時候就會調用drawRect方法15 - (void)drawRect:(CGRect)rect16 {17 18     // 1.取得和當前視圖相關聯的圖形上下文(因為圖形上下文決定繪製的輸出目標)/19     // 如果是在drawRect方法中調用UIGraphicsGetCurrentContext方法擷取出來的就是Layer的上下文20     CGContextRef  ctx=UIGraphicsGetCurrentContext();//不需要*,同id21     22     // 2.繪圖(繪製直線), 儲存繪圖資訊23     // 設定起點24     CGContextMoveToPoint(ctx, 20, 100);25     //設定終點26     CGContextAddLineToPoint(ctx, 300, 100);27     28     29     //設定繪圖的狀態30     //設定線條的顏色為藍色31     CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);32     //設定線條的寬度33     CGContextSetLineWidth(ctx, 15);34     //設定線條起點和終點的樣式為圓角35     CGContextSetLineCap(ctx, kCGLineCapRound);36     //設定線條的轉角的樣式為圓角37     CGContextSetLineJoin(ctx, kCGLineJoinRound);38     //3.渲染(繪製出一條空心的線)39     CGContextStrokePath(ctx);40     41 //    //注意線條不能渲染為實心的42 //    CGContextFillPath(ctx);43     44     45     46     //設定第二條線47     //設定第二條線的起點48     CGContextMoveToPoint(ctx, 50, 200);49     //設定第二天線的終點(自動把上一條直線的終點當做起點)50     CGContextAddLineToPoint(ctx, 300, 60);51     52     //設定繪圖的狀態53 //    CGContextSetRGBStrokeColor(ctx, 1.0, 0.7, 0.3, 1.0);54     //第二種設定顏色的方式55     [[UIColor grayColor] set];56     //設定線條的寬度57     CGContextSetLineWidth(ctx, 10);58     //設定線條的起點和終點的樣式59     CGContextSetLineCap(ctx, kCGLineCapButt);60     61     //渲染第二條線的圖形到view上62     //繪製一條空心的線63     CGContextStrokePath(ctx);64 }65 
畫三角形

其實就是把線關閉了 一句代碼
//關閉起點和終點

CGContextClosePath(ctx);

效果:

- (void)drawRect:(CGRect)rect15 {16     //1.獲得圖形上下文17     CGContextRef ctx=UIGraphicsGetCurrentContext();18     19     //2.繪製三角形20     //設定起點21     CGContextMoveToPoint(ctx, 20, 100);22     //設定第二個點23     CGContextAddLineToPoint(ctx, 40, 300);24     //設定第三個點25     CGContextAddLineToPoint(ctx, 200, 200);26     //設定終點27 //     CGContextAddLineToPoint(ctx, 20, 100);28     //關閉起點和終點29     CGContextClosePath(ctx);30     31     // 3.渲染圖形到layer上32     CGContextStrokePath(ctx);33     34 }
畫四邊形

效果:

- (void)drawRect:(CGRect)rect15 {16 17     //1.擷取圖形上下文18     CGContextRef ctx=UIGraphicsGetCurrentContext();19     //2.畫四邊形20     CGContextAddRect(ctx, CGRectMake(20, 20, 150, 100));21     22     // 如果要設定繪圖的狀態必須在渲染之前23     //    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);24     // 繪製什麼類型的圖形(空心或者實心).就要通過什麼類型的方法設定狀態25     //    CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);26     27     // 調用OC的方法設定繪圖的顏色28     //    [[UIColor purpleColor] setFill];29     //    [[UIColor blueColor] setStroke];30     // 調用OC的方法設定繪圖顏色(同時設定了實心和空心)31     //    [[UIColor greenColor] set];32     [[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] set];33     34     35     //3.渲染圖形到layer上36     //空心的37     CGContextStrokePath(ctx);38     //實心的39 //    CGContextFillPath(ctx);40     41 }
畫圓

效果:最簡單的圓形

- (void)drawRect:(CGRect)rect{    // 1.擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 畫圓    CGContextAddArc(ctx, 100, 100, 50, 0, 2 * M_PI, 0);    // 3.渲染 (注意, 畫線只能通過空心來畫)//    CGContextFillPath(ctx);    CGContextStrokePath(ctx);}

如果改成CGContextFillPath(ctx);
那麼將會變成實心圓。前後聯絡下就都知道了,不多說了。

效果:畫橢圓

// 畫橢圓    // 1.擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.畫圓    CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 100, 230));    [[UIColor purpleColor] set];    // 3.渲染    //    CGContextStrokePath(ctx);    CGContextFillPath(ctx);
畫圓弧

效果:

 // 畫圓弧    // 1.擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.畫圓弧    // x/y 圓心    // radius 半徑    // startAngle 開始的弧度    // endAngle 結束的弧度    // clockwise 畫圓弧的方向 (0 順時針, 1 逆時針)    //    CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI_2, 0);    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);    CGContextClosePath(ctx);    // 3.渲染    //     CGContextStrokePath(ctx);    CGContextFillPath(ctx);
四分之一圓:用於餅圖

效果:

 // 1.擷取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.畫餅狀圖    // 畫線    CGContextMoveToPoint(ctx, 100, 100);    CGContextAddLineToPoint(ctx, 100, 150);    // 畫圓弧    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);    //    CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1);    // 關閉路徑    CGContextClosePath(ctx);    [[UIColor brownColor]set];    // 3.渲染 (注意, 畫線只能通過空心來畫)    CGContextFillPath(ctx);    //    CGContextStrokePath(ctx);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.