天天看點

Quartz 各種繪制圖形用法 Quartz 各種繪制圖形用法 分享類型:應用開發相關 - (void)drawRect:(CGRect)rect { CGContextRef Quartz 各種繪制圖形用法   

Quartz 各種繪制圖形用法   

  • 分享類型:應用開發相關

- (void)drawRect:(CGRect)rect 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    //畫一條線 

     CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//線條顔色 

     CGContextMoveToPoint(context, 20, 20); 

     CGContextAddLineToPoint(context, 200,20); 

     CGContextStrokePath(context); 

    //NO.2寫文字 

    CGContextSetLineWidth(context, 1.0); 

    CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5); 

    UIFont  *font = [UIFont boldSystemFontOfSize:18.0]; 

    [@"sdfsdf" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font]; 

    //NO.3畫一個正方形圖形 沒有邊框 

    CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5); 

    CGContextFillRect(context, CGRectMake(2, 2, 270, 270)); 

    CGContextStrokePath(context); 

    //NO.4畫正方形邊框 

    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//線條顔色 

    CGContextSetLineWidth(context, 2.0); 

    CGContextAddRect(context, CGRectMake(2, 2, 270, 270)); 

    CGContextStrokePath(context); 

    //NO.5畫方形背景顔色 

    CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); 

    CGContextScaleCTM(context, 1.0f, -1.0f); 

    UIGraphicsPushContext(context); 

    CGContextSetLineWidth(context,320); 

    CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0); 

    CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460)); 

    UIGraphicsPopContext(); 

    //NO.6橢圓 

     CGRect aRect= CGRectMake(80, 80, 160, 100); 

     CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 

     CGContextSetLineWidth(context, 3.0); 

     CGContextAddEllipseInRect(context, aRect); //橢圓 

     CGContextDrawPath(context, kCGPathStroke); 

    //NO.7 

    CGContextBeginPath(context); 

    CGContextSetRGBStrokeColor(context, 0, 0, 1, 1); 

    CGContextMoveToPoint(context, 100, 100); 

    CGContextAddArcToPoint(context, 50, 100, 50, 150, 50); 

    CGContextStrokePath(context); 

    //NO.8漸變 

    CGContextClip(context); 

    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 

    CGFloat colors[] = 

    { 

        204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, 

        29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00, 

        0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00, 

    }; 

    CGGradientRef gradient = CGGradientCreateWithColorComponents 

    (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4)); 

    CGColorSpaceRelease(rgb); 

    CGContextDrawLinearGradient(context, gradient,CGPointMake 

                                (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height), 

                                kCGGradientDrawsBeforeStartLocation); 

    //NO.9四條線畫一個正方形 

    //畫線 

        UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 

    CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 

       CGContextSetFillColorWithColor(context, aColor.CGColor); 

    CGContextSetLineWidth(context, 4.0); 

    CGPoint aPoints[5]; 

    aPoints[0] =CGPointMake(60, 60); 

    aPoints[1] =CGPointMake(260, 60); 

    aPoints[2] =CGPointMake(260, 300); 

    aPoints[3] =CGPointMake(60, 300); 

    aPoints[4] =CGPointMake(60, 60); 

    CGContextAddLines(context, aPoints, 5); 

    CGContextDrawPath(context, kCGPathStroke); //開始畫線 

    // NO.10 

    UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 

    CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 

    CGContextSetFillColorWithColor(context, aColor.CGColor); 

    //橢圓 

    CGRect aRect= CGRectMake(80, 80, 160, 100); 

    CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 

    CGContextSetLineWidth(context, 3.0); 

      CGContextSetFillColorWithColor(context, aColor.CGColor); 

       CGContextAddRect(context, rect); //矩形 

    CGContextAddEllipseInRect(context, aRect); //橢圓 

    CGContextDrawPath(context, kCGPathStroke); 

    //NO.11 

     畫一個實心的圓 

     CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100)); 

    //NO.12 

     畫一個菱形 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGContextMoveToPoint(context, 100, 100); 

    CGContextAddLineToPoint(context, 150, 150); 

    CGContextAddLineToPoint(context, 100, 200); 

    CGContextAddLineToPoint(context, 50, 150); 

    CGContextAddLineToPoint(context, 100, 100); 

    CGContextStrokePath(context); 

    //NO.13 畫矩形 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGRect rectangle = CGRectMake(60,170,200,80); 

    CGContextAddRect(context, rectangle); 

    CGContextStrokePath(context); 

    //橢圓 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGRect rectangle = CGRectMake(60,170,200,80); 

    CGContextAddEllipseInRect(context, rectangle); 

    CGContextStrokePath(context); 

    //用紅色填充了一段路徑: 

    CGContextMoveToPoint(context, 100, 100); 

    CGContextAddLineToPoint(context, 150, 150); 

    CGContextAddLineToPoint(context, 100, 200); 

    CGContextAddLineToPoint(context, 50, 150); 

    CGContextAddLineToPoint(context, 100, 100); 

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 

    CGContextFillPath(context); 

    //填充一個藍色邊的紅色矩形 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGRect rectangle = CGRectMake(60,170,200,80); 

    CGContextAddRect(context, rectangle); 

    CGContextStrokePath(context); 

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 

    CGContextFillRect(context, rectangle); 

    //畫弧 

     //弧線的是通過指定兩個切點,還有角度,調用CGContextAddArcToPoint()繪制 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGContextMoveToPoint(context, 100, 100); 

    CGContextAddArcToPoint(context, 100,200, 300,200, 100); 

    CGContextStrokePath(context); 

   // 繪制貝茲曲線 

    //貝茲曲線是通過移動一個起始點,然後通過兩個控制點,還有一個中止點,調用CGContextAddCurveToPoint() 函數繪制 

    CGContextSetLineWidth(context, 2.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGContextMoveToPoint(context, 10, 10); 

    CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400); 

    CGContextStrokePath(context); 

    //繪制二次貝茲曲線 

      CGContextSetLineWidth(context, 2.0); 

      CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

      CGContextMoveToPoint(context, 10, 200); 

      CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 

      CGContextStrokePath(context); 

    //繪制虛線 

    CGContextSetLineWidth(context, 5.0); 

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 

    CGFloat dashArray[] = {2,6,4,2}; 

    CGContextSetLineDash(context, 3, dashArray, 4);//跳過3個再畫虛線,是以剛開始有6-(3-2)=5個虛點 

    CGContextMoveToPoint(context, 10, 200); 

    CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 

    CGContextStrokePath(context); 

//繪制圖檔 

    NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 

    UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath]; 

    //[myImageObj drawAtPoint:CGPointMake(0, 0)]; 

    [myImageObj drawInRect:CGRectMake(0, 0, 320, 480)]; 

    NSString *s = @"我的小狗"; 

    [s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]]; 

  // 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 

    UIImage *img = [UIImage imageWithContentsOfFile:path]; 

    CGImageRef image = img.CGImage; 

    CGContextSaveGState(context); 

    CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 

    CGContextDrawImage(context, touchRect, image); 

    CGContextRestoreGState(context); 

    //NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 

    UIImage *img = [UIImage imageWithContentsOfFile:path]; 

    CGImageRef image = img.CGImage; 

    CGContextSaveGState(context); 

    CGContextRotateCTM(context, M_PI); 

    CGContextTranslateCTM(context, -img.size.width, -img.size.height); 

    CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 

    CGContextDrawImage(context, touchRect, image); 

    CGContextRestoreGState(context);*/ 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 

    UIImage *img = [UIImage imageWithContentsOfFile:path]; 

    CGImageRef image = img.CGImage; 

    CGContextSaveGState(context); 

    CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI); 

    myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height); 

    CGContextConcatCTM(context, myAffine); 

    CGContextRotateCTM(context, M_PI); 

    CGContextTranslateCTM(context, -img.size.width, -img.size.height); 

    CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 

    CGContextDrawImage(context, touchRect, image); 

    CGContextRestoreGState(context); 

繼續閱讀