畫虛線

來源:互聯網
上載者:User

標籤:etop   return   起點   img   draw   ram   bsp   多少   之間   

繪製一條虛線,此方法可以寫到UIImage的分類中,直接寫成類方法調用即可。

 1 - (UIImage *)drawDashImageWithFrame:(CGRect)frame andLineColor:(UIColor *)lineColor 2 { 3     // 開啟圖形上下文 4     UIGraphicsBeginImageContext(frame.size); 5      6     // 擷取上下文 7     CGContextRef cxtRef = UIGraphicsGetCurrentContext(); 8      9     // 設定線頭樣式10     CGContextSetLineCap(cxtRef, kCGLineCapRound);11     12     // 繪製3個點,跳過2個點,繪製3個點,跳過3個點,繪製2個點,如此反覆13     // CGFloat lengths[] = {3,2,3};14     // 6是每個虛線的長度,5是虛線之間的間隔15     CGFloat lengths[] = {6,5};16     17     // 設定線寬(向兩邊擴充)18     CGContextSetLineWidth(cxtRef,1);19     20     // 設定線條渲染顏色21     [lineColor setStroke];22     23     // 畫虛線24     // 參數2:第一次繪製的時候跳過多少個點25     // 參數3:數組26     // 參數4:數組長度27     CGContextSetLineDash(cxtRef, 0,lengths, 2);28     29     // 開始畫線-起點30     CGContextMoveToPoint(cxtRef, 0.0, 0.0);31     32     // 線束畫線-終點33     CGContextAddLineToPoint(cxtRef, [UIScreen mainScreen].bounds.size.width, 0.0);34     35     // 設定描邊36     CGContextStrokePath(cxtRef);37     38     // 從圖形上下文中擷取圖片39     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();40     41     // 關閉圖形上下文42     UIGraphicsEndImageContext();43     44     return img;45 }

 

畫虛線

聯繫我們

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