QuartzD虚线及虚线的用处

来源:互联网 发布:本地网络ip 编辑:程序博客网 时间:2024/06/07 10:54

画虚线需要用到函数:

CGContextSetLineDash

此函数需要四个参数:

  • context – 这个不用多说
  • phase - 稍后再说
  • lengths – 指明虚线是如何交替绘制,具体看例子
  • count – lengths数组的长度

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. CGContextRef context =UIGraphicsGetCurrentContext();  
  2. CGContextBeginPath(context);  
  3. CGContextSetLineWidth(context, 2.0);  
  4. CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);  
  5. CGFloat lengths[] = {10,10};  
  6. CGContextSetLineDash(context, 0, lengths,2);  
  7. CGContextMoveToPoint(context, 10.0, 20.0);  
  8. CGContextAddLineToPoint(context, 310.0,20.0);  
  9. CGContextStrokePath(context);  
  10. CGContextClosePath(context);  

lengths的值{10,10}表示先绘制10个点,再跳过10个点,如此反复,如图:


如果把lengths值改为{10, 20, 10},则表示先绘制10个点,跳过20个点,绘制10个点,跳过10个点,再绘制20个点,如此反复,如图:

注意count的值等于lengths数组的长度

phase参数表示在第一个虚线绘制的时候跳过多少个点,举例说明:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. CGFloat lengths[] = {10,5};  
  2. CGContextSetLineDash(context, 0, lengths, 2);    
  3. CGContextMoveToPoint(context, 0.0, 20.0);    
  4. CGContextAddLineToPoint(context, 310.0, 20.0);     
  5. CGContextStrokePath(context);  
  6.                           
  7. CGContextSetLineDash(context, 5, lengths, 2);  
  8. CGContextMoveToPoint(context, 0.0, 40.0);    
  9. CGContextAddLineToPoint(context, 310.0, 40.0);  
  10. CGContextStrokePath(context);             
  11.                                               
  12. CGContextSetLineDash(context, 8, lengths, 2);     
  13. CGContextMoveToPoint(context, 0.0, 60.0);             
  14. CGContextAddLineToPoint(context, 310.0, 60.);             
  15. CGContextStrokePath(context);   
如图显示:


由于lengths值为{10,5},第一条线就是绘制10,跳过5,反复绘制。

第二条线的phase值为5,则首先绘制【10减去5】,再跳过5,绘制10,反复绘制。

第三条给也如此,先绘制2,再跳过5,如此反复。


可以用虚线定义复杂的UI,比如表格,直方图等等:


父View代码:

 sheetView = [[SheetViewalloc]initWithFrame:CGRectMake(150,TITLE_VIEW_H +160, [TFUtilsgetScreenSize].width -2*150, [TFUtilsgetScreenSize].height -TITLE_VIEW_H -BOTTOM_TOOL_BAR -2*160)];

    sheetView.linesGraph =NO;

    sheetView.fistArray =@[[NSNumbernumberWithInt:12],[NSNumbernumberWithInt:8],[NSNumbernumberWithInt:1],[NSNumbernumberWithInt:16],[NSNumbernumberWithInt:19],[NSNumbernumberWithInt:32.0],[NSNumbernumberWithInt:22],[NSNumbernumberWithInt:-12],[NSNumbernumberWithInt:16],[NSNumbernumberWithInt:19],[NSNumbernumberWithInt:22],[NSNumbernumberWithInt:32.0]];

    sheetView.secondArray =@[[NSNumbernumberWithInt:2],[NSNumbernumberWithInt:-18],[NSNumbernumberWithInt:-5],[NSNumbernumberWithInt:12],[NSNumbernumberWithInt:13],[NSNumbernumberWithInt:30],[NSNumbernumberWithInt:19],[NSNumbernumberWithInt:-10],[NSNumbernumberWithInt:10],[NSNumbernumberWithInt:10],[NSNumbernumberWithInt:32.0],[NSNumbernumberWithInt:12]];

    [self.viewaddSubview:sheetView];

直方图:

- (id)initWithFrame:(CGRect)frame {

    if (self = [superinitWithFrame:frame]) {

        // Initialization code

        self.backgroundColor = [UIColorclearColor];

    }

    returnself;

}


//200px = 100%

- (void)drawRect:(CGRect)rect {

    double maxY =0;

    bool  minusValue =NO;

    bool nothingShow =NO;

    

    for (NSNumber *numberin_fistArray) {

        if (maxY <abs([numberdoubleValue])) {

            maxY = abs([numberdoubleValue]);

        }

        if ([numberdoubleValue] <0) {

            minusValue = YES;

        }

    }

    for (NSNumber *numberin_secondArray) {

        if (maxY <abs([numberdoubleValue])) {

            maxY = abs([numberdoubleValue]);

        }

        if ([numberdoubleValue] <0) {

            minusValue = YES;

        }

    }

    

    if (maxY ==0) {

        maxY = 100;

        nothingShow = YES;

    }

    

    //coefficient

    double coefficient =100/maxY;

    

    UIColor *currentColor = [UIColorblackColor];

    

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextBeginPath(context);// <---- this

    CGContextSetLineWidth(context,2.0);

    CGContextSetStrokeColorWithColor(context, currentColor.CGColor);

    

    // line X

    CGContextSetStrokeColorWithColor(context, [UIColorblackColor].CGColor);

    CGContextMoveToPoint(context,10,150);

    CGContextAddLineToPoint(context,650,150);

    CGContextAddLineToPoint(context,640,144);

    CGContextAddLineToPoint(context,650,150);

    CGContextAddLineToPoint(context,640,156);

    CGContextStrokePath(context);

    

    //Create 5 strike line for see difference

    //And for +  and -

    int halfGraph =150;

    double yNumber =0;

    for (int i =0; i<2; i++) {

        for (int j =0; j<5; j++) {

            halfGraph -= 20;

            yNumber +=maxY/5;

            CGFloat dashes[] = {1,1 };//绘制一个点跳过一个点

            CGContextSetLineDash( context,0.0, dashes,2 );

            CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);

            CGContextMoveToPoint(context,10,halfGraph);

            CGContextAddLineToPoint(context,650, halfGraph);

            CGContextStrokePath(context);

            

            UILabel *gradationLabel = [[UILabelalloc]initWithFrame:CGRectMake(-25, halfGraph,50, 20)];

            gradationLabel.backgroundColor = [UIColorclearColor];

            gradationLabel.textAlignment =NSTextAlignmentRight;

            [gradationLabel setFont:[UIFontsystemFontOfSize:13]];

            [gradationLabel setAdjustsFontSizeToFitWidth:YES];//根据字数多少来自动显示,当文本多的时候,自动调整字体大小以适应UILable

            

            if (!nothingShow) {

                if (!minusValue && i >0) {

                    

                } else{

                    gradationLabel.text = [NSStringstringWithFormat:yNumber == (int)yNumber?@"%.0f":@"%.0f",yNumber];

                }

            }

            

            

            gradationLabel.textColor = [UIColorgrayColor];

            [selfaddSubview:gradationLabel];

        }

        halfGraph = 270;

        yNumber = -maxY-maxY/5;

        

        //If Graph don't have < 0 value  No need wite numbers with minus (i = 2 exit loop)

    }

    

    //Create 12 strike line for month

    

    halfGraph = 4;

    if (_months ==nil) {

        _months = [[NSArrayalloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil];

    }

    

    for (int i =0; i<12; i++) {

        halfGraph += 50;

        CGFloat dashes[] = {1,1 };

        CGContextSetLineDash( context,0.0, dashes,2 );

        CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);

        CGContextMoveToPoint(context, halfGraph,40);

        CGContextAddLineToPoint(context, halfGraph,260);

        CGContextStrokePath(context);

        

        UILabel *gradationLabel = [[UILabelalloc]initWithFrame:CGRectMake(halfGraph-40,260,50,20)];

        gradationLabel.backgroundColor = [UIColorclearColor];

        gradationLabel.textAlignment =NSTextAlignmentRight;

        [gradationLabel setFont:[UIFontsystemFontOfSize:13]];

        [gradationLabel setAdjustsFontSizeToFitWidth:YES];

        gradationLabel.text = [NSStringstringWithFormat:@"%@",[_monthsobjectAtIndex:i]];

        gradationLabel.textColor = [UIColorgrayColor];

        [selfaddSubview:gradationLabel];

    }

    

    CGContextSetLineDash(context,0,NULL,0);

    UILabel *xLabel = [[UILabelalloc]initWithFrame:CGRectMake(-15,18,100,30)];

    xLabel.backgroundColor = [UIColorclearColor];

    [xLabel setFont:[UIFontsystemFontOfSize:14]];

    xLabel.textColor = [UIColordarkGrayColor];

    xLabel.text =@"Money";

    [selfaddSubview:xLabel];

    

    UILabel *zeroLabel = [[UILabelalloc]initWithFrame:CGRectMake(15,150,30,20)];

    zeroLabel.backgroundColor = [UIColorclearColor];

    zeroLabel.textColor = [UIColorgrayColor];

    [zeroLabel setFont:[UIFontsystemFontOfSize:13]];

    zeroLabel.text =@"0";

    [selfaddSubview:zeroLabel];

    

    // line Y

    CGContextSetStrokeColorWithColor(context, [UIColorblackColor].CGColor);

    CGContextMoveToPoint(context,30,250);

    CGContextAddLineToPoint(context,30,50);

    CGContextAddLineToPoint(context,24,60);

    CGContextAddLineToPoint(context,30,50);

    CGContextAddLineToPoint(context,36,60);

    CGContextStrokePath(context);

    

    UILabel *yLabel = [[UILabelalloc]initWithFrame:CGRectMake(635,155,100,30)];

    yLabel.backgroundColor = [UIColorclearColor];

    [yLabel setFont:[UIFontsystemFontOfSize:14]];

    yLabel.textColor = [UIColordarkGrayColor];

    yLabel.text =@"Month";

    [selfaddSubview:yLabel];

    

    CGContextSetLineWidth(context,1.0);

    

    //FirstArray

    

    if (_linesGraph) {

        double demoData[31] = {[[_fistArrayobjectAtIndex:0]doubleValue],[[_fistArrayobjectAtIndex:1]doubleValue],[[_fistArrayobjectAtIndex:2]doubleValue],[[_fistArrayobjectAtIndex:3]doubleValue],[[_fistArrayobjectAtIndex:4]doubleValue],[[_fistArrayobjectAtIndex:5]doubleValue],[[_fistArrayobjectAtIndex:6]doubleValue],[[_fistArrayobjectAtIndex:7]doubleValue],[[_fistArrayobjectAtIndex:8]doubleValue],[[_fistArrayobjectAtIndex:9]doubleValue],[[_fistArrayobjectAtIndex:10]doubleValue],[[_fistArrayobjectAtIndex:11]doubleValue]};

        yLabel.text =@"Month";

        double distanceNextYline =50;

        double bizieLineAligm =20;

        

        double nextX =55;

        double downToXline =150;

        double corectPointer1 = downToXline - demoData[0]*coefficient;

        

        CGMutablePathRef spadePath =CGPathCreateMutable();

        CGPathMoveToPoint(spadePath,NULL, nextX, downToXline - demoData[0]*coefficient);

        

        

        for (int i =1; i < [_fistArraycount]; i++) {

            CGPathAddCurveToPoint(spadePath,NULL, nextX+bizieLineAligm, corectPointer1, nextX+distanceNextYline-bizieLineAligm, downToXline - demoData[i]*coefficient, nextX+distanceNextYline, downToXline - demoData[i]*coefficient);

            nextX +=distanceNextYline;

            corectPointer1 = downToXline - demoData[i]*coefficient;

        }

        [[UIColorcolorWithRed:54.0/255.0fgreen:133.0f/255.0fblue:37.0f/255.0falpha:1.0f]set];

        CGContextSetLineWidth(context,4);

        CGContextAddPath(context, spadePath);

        

        CGContextStrokePath(context);

        

        

        

    } else {

        

        

        if ([_fistArraycount] ==12) {

            double demoData[12] = {[[_fistArrayobjectAtIndex:0]doubleValue],[[_fistArrayobjectAtIndex:1]doubleValue],[[_fistArrayobjectAtIndex:2]doubleValue],[[_fistArrayobjectAtIndex:3]doubleValue],[[_fistArrayobjectAtIndex:4]doubleValue],[[_fistArrayobjectAtIndex:5]doubleValue],[[_fistArrayobjectAtIndex:6]doubleValue],[[_fistArrayobjectAtIndex:7]doubleValue],[[_fistArrayobjectAtIndex:8]doubleValue],[[_fistArrayobjectAtIndex:9]doubleValue],[[_fistArrayobjectAtIndex:10]doubleValue],[[_fistArrayobjectAtIndex:11]doubleValue]};

            

            int coor =50;

            for (int i =0; i<12; i++) {

                

                

                CGContextSetStrokeColorWithColor(context, [UIColorcolorWithRed:96.0f/255.0fgreen:172.0f/255.0fblue:96.0f/255.0falpha:1].CGColor);

                CGContextSetRGBFillColor(context,96.0f/255.0f,172.0f/255.0f,96.0f/255.0f,1.0);

                

                CGContextSetShadow(context,CGSizeMake(2,2) , 2);

                CGMutablePathRef pathRef =CGPathCreateMutable();

                CGPathMoveToPoint(pathRef,NULL, coor,150);

                CGPathAddLineToPoint(pathRef,NULL, coor, -demoData[i]*coefficient+150);

                CGPathAddLineToPoint(pathRef,NULL, coor+15, -demoData[i]*coefficient+150);

                CGPathAddLineToPoint(pathRef,NULL, coor+15,150);

                CGPathCloseSubpath(pathRef);

                CGContextAddPath(context, pathRef);

                CGContextFillPath(context);

                CGContextAddPath(context, pathRef);

                CGContextStrokePath(context);

                

                

                CGPathRelease(pathRef);

                int negCor;

                if (demoData[i]>0) {

                    negCor = 20;

                } else {

                    negCor = 0;

                }

                UILabel *oneLabel = [[UILabelalloc]initWithFrame:CGRectMake(coor+2.5, -demoData[i]*coefficient+150-negCor,50,20)];

                oneLabel.backgroundColor = [UIColorclearColor];

                

                

                oneLabel.textColor = [UIColorcolorWithRed:96.0f/255.0fgreen:172.0f/255.0fblue:96.0f/255.0falpha:1];

                

                oneLabel.text = [NSStringstringWithFormat:demoData[i] == (int)demoData[i]?@"%.0f":@"%.0f",demoData[i]];

                oneLabel.shadowColor = [UIColordarkGrayColor];

                oneLabel.shadowOffset =CGSizeMake(1.0,1.0);

                [oneLabel setFont:[UIFontsystemFontOfSize:14]];

                [oneLabel setAdjustsFontSizeToFitWidth:YES];

                [selfaddSubview:oneLabel];

                

                coor += 50;

            }

        }

        

    }

    

    

    //SecondArray

    

    

    if (_linesGraph) {

        double demoData[31] = {[[_secondArrayobjectAtIndex:0]doubleValue],[[_secondArrayobjectAtIndex:1]doubleValue],[[_secondArrayobjectAtIndex:2]doubleValue],[[_secondArrayobjectAtIndex:3]doubleValue],[[_secondArrayobjectAtIndex:4]doubleValue],[[_secondArrayobjectAtIndex:5]doubleValue],[[_secondArrayobjectAtIndex:6]doubleValue],[[_secondArrayobjectAtIndex:7]doubleValue],[[_secondArrayobjectAtIndex:8]doubleValue],[[_secondArrayobjectAtIndex:9]doubleValue],[[_secondArrayobjectAtIndex:10]doubleValue],[[_secondArrayobjectAtIndex:11]doubleValue]};

        yLabel.text =@"Month";

        double distanceNextYline =50;

        double bizieLineAligm =20;

        

        double nextX =55;

        double downToXline =150;

        double corectPointer1 = downToXline - demoData[0]*coefficient;

        //        NSLog(@"FirstPOint:  %f , %f",nextX,downToXline - demoData[0]*coefficient);

        

        CGMutablePathRef spadePath =CGPathCreateMutable();

        CGPathMoveToPoint(spadePath,NULL, nextX, downToXline - demoData[0]*coefficient);

        

        

        for (int i =1; i < [_fistArraycount]; i++) {

            CGPathAddCurveToPoint(spadePath,NULL, nextX+bizieLineAligm, corectPointer1, nextX+distanceNextYline-bizieLineAligm, downToXline - demoData[i]*coefficient, nextX+distanceNextYline, downToXline - demoData[i]*coefficient);

            //            NSLog(@"POint %d: %f , %f %f %f %f %f",i,nextX+bizieLineAligm, corectPointer1, nextX+distanceNextYline-bizieLineAligm, downToXline - demoData[i], nextX+distanceNextYline, downToXline - demoData[i]);

            nextX +=distanceNextYline;

            corectPointer1 = downToXline - demoData[i]*coefficient;

        }

        [[UIColorcolorWithRed:229.0/255.0fgreen:168.0/255.0fblue:10.0/255.0falpha:1.0f]set];

        CGContextSetLineWidth(context,4);

        CGContextAddPath(context, spadePath);

        

        CGContextStrokePath(context);

        

        

        

    } else {

        

        

        

        if ([_secondArraycount] ==12) {

            //seconArray

            double demoData2[12] = {[[_secondArrayobjectAtIndex:0]doubleValue],[[_secondArrayobjectAtIndex:1]doubleValue],[[_secondArrayobjectAtIndex:2]doubleValue],[[_secondArrayobjectAtIndex:3]doubleValue],[[_secondArrayobjectAtIndex:4]doubleValue],[[_secondArrayobjectAtIndex:5]doubleValue],[[_secondArrayobjectAtIndex:6]doubleValue],[[_secondArrayobjectAtIndex:7]doubleValue],[[_secondArrayobjectAtIndex:8]doubleValue],[[_secondArrayobjectAtIndex:9]doubleValue],[[_secondArrayobjectAtIndex:10]doubleValue],[[_secondArrayobjectAtIndex:11]doubleValue]};

            

            

            int coor =40;

            for (int i =0; i<12; i++) {

                CGContextSetStrokeColorWithColor(context, [UIColorcolorWithRed:229.0f/255.0fgreen:168.0f/255.0fblue:10.0f/255.0falpha:1].CGColor);

                CGContextSetRGBFillColor(context,229.0f/255.0f,168.0f/255.0f,10.0f/255.0f,1.0);

                CGMutablePathRef pathRef =CGPathCreateMutable();

                CGPathMoveToPoint(pathRef,NULL, coor,150);

                CGPathAddLineToPoint(pathRef,NULL, coor, -demoData2[i]*coefficient+150);

                CGPathAddLineToPoint(pathRef,NULL, coor+15, -demoData2[i]*coefficient+150);

                CGPathAddLineToPoint(pathRef,NULL, coor+15,150);

                CGPathCloseSubpath(pathRef);

                CGContextAddPath(context, pathRef);

                CGContextFillPath(context);

                CGContextAddPath(context, pathRef);

                CGContextStrokePath(context);

                CGContextSetShadow(context,CGSizeMake(2,2) , 2);

                

                CGPathRelease(pathRef);

                int negCor;

                if (demoData2[i]>0) {

                    negCor = 20;

                } else {

                    negCor = 0;

                }

                UILabel *secondLabel = [[UILabelalloc]initWithFrame:CGRectMake(coor-38, -demoData2[i]*coefficient+150-negCor,50,20)];

                secondLabel.textAlignment =NSTextAlignmentRight;

                secondLabel.backgroundColor = [UIColorclearColor];

                secondLabel.textColor = [UIColorcolorWithRed:229.0f/255.0fgreen:168.0f/255.0fblue:10.0f/255.0falpha:1];

                secondLabel.text = [NSStringstringWithFormat:demoData2[i] == (int)demoData2[i]?@"%.0f":@"%.0f",demoData2[i]];

                secondLabel.shadowColor = [UIColordarkGrayColor];

                secondLabel.shadowOffset =CGSizeMake(1.0,1.0);

                [secondLabel setFont:[UIFontsystemFontOfSize:14]];

                [secondLabel setAdjustsFontSizeToFitWidth:YES];

                [selfaddSubview:secondLabel];

                

                coor += 50;

            }

        }

    }


}



0 0
原创粉丝点击