繪圖quartz之漸層

來源:互聯網
上載者:User

標籤:

實現線性漸層   放射狀漸層 自己建立的view中有一個drawRect:(cgrect)rect方法在這個方法裡 可以通過畫圖 將內容顯示在畫板上(即最下邊的view) 漸層的方式分兩種  :1種是線性漸層    1種是放射狀漸層首先先看 線性漸層   1 設定映像的上下文 2 獲得色彩空間  說明他是什麼類型的顏色3 設定開始顏色  結束顏色  並且將他們換成Cg顏色 4 設定漸層   漸層的4個參數  沒有的需要進行建立  2  3 5 繪製線性漸層  繪製放射狀漸層6清理creat建立的對象 用cg// Drawing code
    //獲得當前圖形器的當前的上下文
    CGContextRef context  = UIGraphicsGetCurrentContext();
   
    //建立漸層的色彩空間類型是RGB顏色
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //設定顏色的開始 顏色的結束
    UIColor *beginColor =  [UIColor redColor];
    //將色彩轉換為cgColor//CGFloat是浮點型的基礎資料型別 (Elementary Data Type)
    CGFloat *beginC =CGColorGetComponents([beginColor CGColor]);
    UIColor *endColor = [UIColor yellowColor];
   
   
    CGFloat *endC = CGColorGetComponents([endColor CGColor]);
   
    //將兩個顏色的元素加入到一個Cgfloat數組中
    CGFloat component[8] = {beginC[0],beginC[1],beginC[2],beginC[3],endC[0],endC[1],endC[2],endC[3]};
    //設定顏色的區間 區間範圍是0 1  如果區間沒有你要的顏色 就會執行靠近區間的顏色
    CGFloat location[2] ={0,1};
   
    //設定漸層    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, component, location, 2);     //繪製直線漸層    CGContextDrawLinearGradient(context, gradient, CGPointMake(100, 100), CGPointMake(200, 200), 0);     //繪製放射狀漸層    CGContextDrawRadialGradient(context, gradient, CGPointMake(300, 300), 50, CGPointMake(300, 300), 60, kCGGradientDrawsBeforeStartLocation);     //清理
    CGColorSpaceRelease(colorSpace);    CGGradientRelease(gradient);      

繪圖quartz之漸層

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.