IOS Quartz 各种绘制图形用法

来源:互联网 发布:免费动画视频制作软件 编辑:程序博客网 时间:2024/05/20 20:18
  1. - (void)drawRect:(CGRect)rect  
  2. {  
  3.     CGContextRef context = UIGraphicsGetCurrentContext();  
  4.        
  5.    
  6.        
  7.     /*NO.1画一条线 
  8.        
  9.      CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
  10.      CGContextMoveToPoint(context, 20, 20); 
  11.      CGContextAddLineToPoint(context, 200,20); 
  12.      CGContextStrokePath(context); 
  13.     */  
  14.    
  15.        
  16.        
  17.     /*NO.2写文字 
  18.        
  19.     CGContextSetLineWidth(context, 1.0); 
  20.     CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5); 
  21.     UIFont  *font = [UIFont boldSystemFontOfSize:18.0]; 
  22.     [@"公司:公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font]; 
  23.     */  
  24.    
  25.        
  26.     /*NO.3画一个正方形图形 没有边框 
  27.   
  28.     CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5); 
  29.     CGContextFillRect(context, CGRectMake(2, 2, 270, 270)); 
  30.     CGContextStrokePath(context); 
  31.     */  
  32.     
  33.        
  34.     /*NO.4画正方形边框 
  35.       
  36.     CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色 
  37.     CGContextSetLineWidth(context, 2.0); 
  38.     CGContextAddRect(context, CGRectMake(2, 2, 270, 270)); 
  39.     CGContextStrokePath(context); 
  40.     */  
  41.    
  42.        
  43.     /*NO.5画方形背景颜色 
  44.        
  45.     CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); 
  46.     CGContextScaleCTM(context, 1.0f, -1.0f); 
  47.     UIGraphicsPushContext(context); 
  48.     CGContextSetLineWidth(context,320); 
  49.     CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0); 
  50.     CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460)); 
  51.     UIGraphicsPopContext(); 
  52.     */  
  53.    
  54.     /*NO.6椭圆 
  55.        
  56.      CGRect aRect= CGRectMake(80, 80, 160, 100); 
  57.      CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
  58.      CGContextSetLineWidth(context, 3.0); 
  59.      CGContextAddEllipseInRect(context, aRect); //椭圆 
  60.      CGContextDrawPath(context, kCGPathStroke); 
  61.     */  
  62.    
  63.     /*NO.7 
  64.     CGContextBeginPath(context); 
  65.     CGContextSetRGBStrokeColor(context, 0, 0, 1, 1); 
  66.     CGContextMoveToPoint(context, 100, 100); 
  67.     CGContextAddArcToPoint(context, 50, 100, 50, 150, 50); 
  68.     CGContextStrokePath(context); 
  69.     */  
  70.    
  71.     /*NO.8渐变 
  72.     CGContextClip(context); 
  73.     CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 
  74.     CGFloat colors[] = 
  75.     { 
  76.         204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, 
  77.         29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00, 
  78.         0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00, 
  79.     }; 
  80.     CGGradientRef gradient = CGGradientCreateWithColorComponents 
  81.     (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4)); 
  82.     CGColorSpaceRelease(rgb); 
  83.     CGContextDrawLinearGradient(context, gradient,CGPointMake 
  84.                                 (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height), 
  85.                                 kCGGradientDrawsBeforeStartLocation); 
  86.      */  
  87.        
  88.       
  89.     /* NO.9四条线画一个正方形 
  90.     //画线 
  91.         UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
  92.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
  93.        CGContextSetFillColorWithColor(context, aColor.CGColor); 
  94.     CGContextSetLineWidth(context, 4.0); 
  95.     CGPoint aPoints[5]; 
  96.     aPoints[0] =CGPointMake(60, 60); 
  97.     aPoints[1] =CGPointMake(260, 60); 
  98.     aPoints[2] =CGPointMake(260, 300); 
  99.     aPoints[3] =CGPointMake(60, 300); 
  100.     aPoints[4] =CGPointMake(60, 60); 
  101.     CGContextAddLines(context, aPoints, 5); 
  102.     CGContextDrawPath(context, kCGPathStroke); //开始画线 
  103.      */  
  104.        
  105.        
  106.        
  107.     /*  NO.10 
  108.     UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0]; 
  109.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0); 
  110.     CGContextSetFillColorWithColor(context, aColor.CGColor); 
  111.     //椭圆 
  112.     CGRect aRect= CGRectMake(80, 80, 160, 100); 
  113.     CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
  114.     CGContextSetLineWidth(context, 3.0); 
  115.       CGContextSetFillColorWithColor(context, aColor.CGColor); 
  116.        CGContextAddRect(context, rect); //矩形 
  117.     CGContextAddEllipseInRect(context, aRect); //椭圆 
  118.     CGContextDrawPath(context, kCGPathStroke); 
  119.      */  
  120.    
  121.        
  122.        
  123.     /*  NO.11 
  124.      画一个实心的圆 
  125.    
  126.      CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100)); 
  127.     */  
  128.        
  129.        
  130.        
  131.     /*NO.12 
  132.      画一个菱形 
  133.     CGContextSetLineWidth(context, 2.0); 
  134.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  135.     CGContextMoveToPoint(context, 100, 100); 
  136.     CGContextAddLineToPoint(context, 150, 150); 
  137.     CGContextAddLineToPoint(context, 100, 200); 
  138.     CGContextAddLineToPoint(context, 50, 150); 
  139.     CGContextAddLineToPoint(context, 100, 100); 
  140.     CGContextStrokePath(context); 
  141.      */  
  142.    
  143.     /*NO.13 画矩形 
  144.     CGContextSetLineWidth(context, 2.0); 
  145.   
  146.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  147.   
  148.     CGRect rectangle = CGRectMake(60,170,200,80); 
  149.   
  150.     CGContextAddRect(context, rectangle); 
  151.       
  152.     CGContextStrokePath(context); 
  153.      */  
  154.        
  155.       
  156.     /*椭圆 
  157.     CGContextSetLineWidth(context, 2.0); 
  158.   
  159.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  160.   
  161.     CGRect rectangle = CGRectMake(60,170,200,80); 
  162.   
  163.     CGContextAddEllipseInRect(context, rectangle); 
  164.       
  165.     CGContextStrokePath(context); 
  166.      */  
  167.        
  168.     /*用红色填充了一段路径: 
  169.       
  170.     CGContextMoveToPoint(context, 100, 100); 
  171.     CGContextAddLineToPoint(context, 150, 150); 
  172.     CGContextAddLineToPoint(context, 100, 200); 
  173.     CGContextAddLineToPoint(context, 50, 150); 
  174.     CGContextAddLineToPoint(context, 100, 100); 
  175.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
  176.     CGContextFillPath(context); 
  177.     */  
  178.        
  179.     /*填充一个蓝色边的红色矩形 
  180.     CGContextSetLineWidth(context, 2.0); 
  181.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  182.     CGRect rectangle = CGRectMake(60,170,200,80); 
  183.     CGContextAddRect(context, rectangle); 
  184.     CGContextStrokePath(context); 
  185.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
  186.     CGContextFillRect(context, rectangle); 
  187.     */  
  188.        
  189.     /*画弧 
  190.      //弧线的是通过指定两个切点,还有角度,调用CGContextAddArcToPoint()绘制 
  191.     CGContextSetLineWidth(context, 2.0); 
  192.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  193.     CGContextMoveToPoint(context, 100, 100); 
  194.     CGContextAddArcToPoint(context, 100,200, 300,200, 100); 
  195.     CGContextStrokePath(context); 
  196.     */  
  197.       
  198.        
  199.     /* 
  200.     绘制贝兹曲线 
  201.     //贝兹曲线是通过移动一个起始点,然后通过两个控制点,还有一个中止点,调用CGContextAddCurveToPoint() 函数绘制 
  202.     CGContextSetLineWidth(context, 2.0); 
  203.   
  204.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  205.   
  206.     CGContextMoveToPoint(context, 10, 10); 
  207.   
  208.     CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400); 
  209.       
  210.     CGContextStrokePath(context); 
  211.      */  
  212.        
  213.     /*绘制二次贝兹曲线 
  214.       
  215.       CGContextSetLineWidth(context, 2.0); 
  216.   
  217.       CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  218.   
  219.       CGContextMoveToPoint(context, 10, 200); 
  220.   
  221.       CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
  222.       
  223.       CGContextStrokePath(context); 
  224.      */  
  225.        
  226.     /*绘制虚线 
  227.     CGContextSetLineWidth(context, 5.0); 
  228.   
  229.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
  230.   
  231.     CGFloat dashArray[] = {2,6,4,2}; 
  232.   
  233.     CGContextSetLineDash(context, 3, dashArray, 4);//跳过3个再画虚线,所以刚开始有6-(3-2)=5个虚点 
  234.       
  235.     CGContextMoveToPoint(context, 10, 200); 
  236.       
  237.     CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200); 
  238.       
  239.     CGContextStrokePath(context); 
  240.     */  
  241. /*绘制图片 
  242.     NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  243.     UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath]; 
  244.     //[myImageObj drawAtPoint:CGPointMake(0, 0)]; 
  245.     [myImageObj drawInRect:CGRectMake(0, 0, 320, 480)]; 
  246.   
  247.     NSString *s = @"我的小狗"; 
  248.   
  249.     [s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]]; 
  250. */  
  251.        
  252.   /* 
  253.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  254.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  255.     CGImageRef image = img.CGImage; 
  256.     CGContextSaveGState(context); 
  257.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  258.     CGContextDrawImage(context, touchRect, image); 
  259.     CGContextRestoreGState(context); 
  260.    */  
  261.      
  262.        
  263.     /*NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  264.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  265.     CGImageRef image = img.CGImage; 
  266.     CGContextSaveGState(context); 
  267.   
  268.     CGContextRotateCTM(context, M_PI); 
  269.     CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  270.   
  271.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  272.     CGContextDrawImage(context, touchRect, image); 
  273.     CGContextRestoreGState(context);*/  
  274.    
  275. /* 
  276.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"]; 
  277.     UIImage *img = [UIImage imageWithContentsOfFile:path]; 
  278.     CGImageRef image = img.CGImage; 
  279.       
  280.     CGContextSaveGState(context); 
  281.   
  282.     CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI); 
  283.     myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height); 
  284.     CGContextConcatCTM(context, myAffine); 
  285.   
  286.     CGContextRotateCTM(context, M_PI); 
  287.     CGContextTranslateCTM(context, -img.size.width, -img.size.height); 
  288.   
  289.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); 
  290.     CGContextDrawImage(context, touchRect, image); 
  291.     CGContextRestoreGState(context); 
  292. */  
  293. }  
0 0
原创粉丝点击