iOS之CoreGraphics使用(1)

荒凉一梦 提交于 2019-12-07 11:18:08

     CoreGraphics.framework 是iOS 内置的用于画图的框架,可以画自定义的几何图形,它支持图形上下文、加载图像、绘制图像,等等。

      下面是我今天练习的代码:

     (1)代码1:绘制字符串

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIColor * magentaColor = [UIColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f];
    [magentaColor set];
    UIFont * helveticaBold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0f];
    NSString * myString = @"I Learn Really Fast";
    [myString drawAtPoint:CGPointMake(25, 190) withFont:helveticaBold];
}
    (2)代码2:绘制图像

-(void)drawRect:(CGRect)rect
{
    UIImage * image = [UIImage imageNamed:@"xcode.png"];
    if(image != nil)
    {
        NSLog(@"Successfully loaded the image");
    }
    else
    {
        NSLog(@"Failed to load the image");
    }
    
    [image drawAtPoint:CGPointMake(0.0f, 20.0f)];
    [image drawInRect:CGRectMake(50.0f, 10.0f, 40.0f,35.0f)];
    
}
    (3)代码3:绘制线段

-(void)drawRect:(CGRect)rect
{
    [[UIColor brownColor] set];
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(currentContext, 5.0f);
    CGContextMoveToPoint(currentContext, 50.0f, 10.0f);
    CGContextAddLineToPoint(currentContext, 100.0f, 200.0f);
    CGContextStrokePath(currentContext);
}
   (4)绘制两条相连的线段

-(void)drawRect:(CGRect)rect
{
    [[UIColor brownColor] set];
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(currentContext, 5.0f);
    CGContextMoveToPoint(currentContext, 20.0f, 20.0f);
    CGContextAddLineToPoint(currentContext, 100.0f, 100.0f);
    CGContextAddLineToPoint(currentContext, 300.0f, 100.0f);
    CGContextStrokePath(currentContext);
}
   (5)绘制屋顶demo

-(void)drawRect:(CGRect)rect
{
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 40.0f) textToDisplay:@"Miter" lineJoin:kCGLineJoinMiter];
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 180.0f) textToDisplay:@"Bevel" lineJoin:kCGLineJoinBevel];
    [self drawRooftopAtTopPointof:CGPointMake(160.0f, 320.0f) textToDisplay:@"Round" lineJoin:kCGLineJoinRound];
}


-(void)drawRooftopAtTopPointof:(CGPoint)paramTopPoint textToDisplay:(NSString * )paramText lineJoin:(CGLineJoin)paramLineJoin
{
    [[UIColor brownColor] set];
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineJoin(currentContext,paramLineJoin);
    CGContextSetLineWidth(currentContext, 20.0f);
    CGContextMoveToPoint(currentContext, paramTopPoint.x - 140, paramTopPoint.y + 100);
    CGContextAddLineToPoint(currentContext, paramTopPoint.x, paramTopPoint.y);
    CGContextAddLineToPoint(currentContext, paramTopPoint.x + 140, paramTopPoint.y + 100);
    CGContextStrokePath(currentContext);
    [[UIColor blackColor] set];
    [paramText drawAtPoint:CGPointMake(paramTopPoint.x - 40.0f, paramTopPoint.y + 60.0f) withFont:[UIFont boldSystemFontOfSize:30.0f]];
}
   (6)绘制矩形

-(void)drawRect:(CGRect)rect
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
    CGPathAddRect(path, NULL, rectangle);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextAddPath(currentContext, path);
    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
    [[UIColor brownColor] setStroke];
    CGContextSetLineWidth(currentContext, 5.0f);
    CGContextDrawPath(currentContext, kCGPathFillStroke);
    CGPathRelease(path);
    
}
   (7)同时绘制多个矩形

-(void)drawRect:(CGRect)rect
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rectangle1 = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);
    CGRect rectangle2 = CGRectMake(40.0f, 100.0f, 90.0f, 300.0f);
    CGRect rectangles[2] = {rectangle1,rectangle2};
    CGPathAddRects(path, NULL, (const CGRect *)&rectangles, 2);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
    CGContextAddPath(currentContext, path);
    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
    [[UIColor blackColor] setStroke];
    
    CGContextDrawPath(currentContext, kCGPathFillStroke);
    CGPathRelease(path);
    
    
}
  (8)给几何图形添加阴影

-(void)drawRectAtTopOfScreen
{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(currentContext);
    CGContextSetShadowWithColor(currentContext, CGSizeMake(10.0f, 10.0f), 20.0f, [[UIColor grayColor] CGColor]);
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect firstRect = CGRectMake(55.0f, 60.0f, 150.0f, 150.0f);
    CGPathAddRect(path, NULL, firstRect);
    CGContextAddPath(currentContext, path);
    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
    CGContextDrawPath(currentContext, kCGPathFill);
    CGPathRelease(path);
    CGContextRestoreGState(currentContext);
}

-(void)drawRectAtBottomOfScreen
{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect secondRect = CGRectMake(150.0f, 250.0f, 100.0f, 100.0f);
    CGPathAddRect(path, NULL
                  , secondRect);
    CGContextAddPath(currentContext, path);
    [[UIColor purpleColor] setFill];
    CGContextDrawPath(currentContext, kCGPathFill);
    CGPathRelease(path);
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!