iOS点線を引く

5001 ワード

from: http://www.cnblogs.com/qingjoin/archive/2013/09/24/3337488.html
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 320, 20)];
    [self.view addSubview:imageView1];
    
    
    UIGraphicsBeginImageContext(imageView1.frame.size);   //    
    [imageView1.image drawInRect:CGRectMake(0, 0, imageView1.frame.size.width, imageView1.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //        
    
    
    float lengths[] = {10,5};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(line, [UIColor redColor].CGColor);
   
    CGContextSetLineDash(line, 0, lengths, 2);  //   
    CGContextMoveToPoint(line, 0.0, 20.0);    //    
    CGContextAddLineToPoint(line, 310.0, 20.0);
    CGContextStrokePath(line);
    
    imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
from: http://blog.csdn.net/zhangao0086/article/details/7234859
点線を描くには関数が必要です.
CGConteext SetLink Dash
この関数には4つのパラメータが必要です.
context – これは言うまでもなく、です.
phase - 後でまた話します.
lengths – 点線をどのように交互に描きますか?具体的には例を見てください.
  • count – レングス配列の長さ
  • CGConteextRef context =UGraphics GetCurentConttext()   CGConteext Beginnath(context)   CGConteext SetLink Width(context) 2.0)   CGConteext Set Stroke ColorWithColor(context、 [UICColorwhite Color].CGColor;   float lengths[] = {10,10}   CGConteext SetLink Dash(context、 0, lengths、2)   CGConteext MoveToPoint(context、 10.0, 20.0)   CGContectAddline ToPoint(context、 31.0,20.0);   CGConteext StrokePath(context)   CGContectext Close Path(context)   lengthsの値{10,10}は、まず10点を描き、さらに10点をスキップするという意味です.

    lengths値を{10、20、10}に変更すると、まず10点を描き、20点をスキップして10点を描き、10点をスキップして20点を描き、さらに20点を描き、このように繰り返します.

    注意countの値はlengths配列の長さに等しいです.
    phaseパラメータは、最初の破線で描画した時に、いくつの点をスキップしたかを示します.例を挙げて説明します.
    float lengths[] = {10,5}   CGConteext SetLink Dash(context、 0, レングス、 2)     CGConteext MoveToPoint(context、 0.0、 20.0)     CGContectAddline ToPoint(context、 31.0、 20.0)      CGConteext StrokePath(context)                              CGConteext SetLink Dash(context、 5, レングス、 2)   CGConteext MoveToPoint(context、 0.0、 40.0);     CGContectAddline ToPoint(context、 31.0、 40.0);   CGConteext StrokePath(context)                                                             CGConteext SetLink Dash(context、 8, レングス、 2)      CGConteext MoveToPoint(context、 0.0、 60.0);              CGContectAddline ToPoint(context、 31.0、 60.)              CGConteext StrokePath(context)    図のように表示:

    lengthsの値は{10、5}ですので、最初の線は10を描き、5をスキップして繰り返し描きます.
    第二の線のphase値は5で、まず【10から5を引く】を描き、さらに5をスキップして10を描き、繰り返し描きます.
    第三条もそうです.まず2を描き、また5をスキップして繰り返します.