CoreGraphics

1、绘制文本

http://blog.csdn.net/developer_zhang/article/details/8924461

2、绘制图像

- (void)drawRect:(CGRect)rect{
    //初始化UIImage对象
    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, 50.0f)];
    //制定Rect画图
    [image drawInRect:CGRectMake(50.0f,10.0f, 40.0f, 35.0f)];
}


3、画线

- (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];
}
/*
 paramTopPoint:一个点,顶部在这一点
 textToDisplay:内显示的文字
 lineJoin:要使用的接合类型
 kCGLineJoinMiter
 接合点为尖角。这是默认的接合类型。
 kCGLineJoinBevel
 接合点为斜角
 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];
    /* 写文字 */
    CGPoint drawingPoint = CGPointMake(paramTopPoint.x - 40.0f,
                                       paramTopPoint.y + 60.0f);
    [paramText drawAtPoint:drawingPoint withFont:[UIFont boldSystemFontOfSize:30.0f]];
}

4、构造和绘制路径

- (void)drawRect:(CGRect)rect{
    //创建路径 创建一个新的 CGMutablePathRef 类型的可变路径并返回其句柄。
    CGMutablePathRef path = CGPathCreateMutable();
    /* How big is our screen? We want the X to cover the whole screen */
    //范围为整个屏幕
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    //从左上角开始画路径 将路径上当前画笔位置移动到 CGPoint 类型的参数指定的点。
    CGPathMoveToPoint(path, NULL,screenBounds.origin.x, screenBounds.origin.y);
    //从左上角连线到右下角 从画笔当前位置向指定位置绘制一条线段。
    CGPathAddLineToPoint(path,NULL, screenBounds.size.width, screenBounds.size.height);
    //开始另一点从右上角
    CGPathMoveToPoint(path,NULL, screenBounds.size.width, screenBounds.origin.y);
    //从右上角到左下角
    CGPathAddLineToPoint(path,NULL, screenBounds.origin.x, screenBounds.size.height);
    //获得当前图形的上下文
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    /* Add the path to the context so we candraw it later */
    //添加路径到路径上下文中 向图形环境上添加一个路径(由一个路径句柄指定),该路径已经准备好被绘制。
    CGContextAddPath(currentContext,path);
    //设置蓝色
    [[UIColor blueColor] setStroke];
    //画图 在图形环境上绘制指定路径
    /*kCGPathStroke
     画线来标记路径的边界或边缘,使用选中的绘图色。
     kCGPathFill
     用选中的填充色,填充被路径包围的区域。
     kCGPathFillStroke
     组合绘图和填充。用当前填充色填充路径,并用当前绘图色绘制路径边界。下面我们会看到一个使用此方 法的例子。
     */
    CGContextDrawPath(currentContext, kCGPathStroke);
    //释放路径
    CGPathRelease(path);
}

5、绘制矩形

- (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);
}

6、增加阴影

-(void)drawRect:(CGRect)rect{
    [self drawRectAtTopOfScreen];
    //由于第一个矩形画的时候已经有了阴影,所以就算第二个矩形绘图时候没有设置依然有阴影(第二个矩形没有调用CGContextSetShadowWithColor或是CGContextSetShadow)
    [self drawRectAtBottomOfScreen];
}

- (void) drawRectAtTopOfScreen{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //用灰色设置背景颜色
    /*第二个参数:阴影的位移,由 CGSize 类型值指定,从每个形状要应用阴影的右下部分开始。位移的 x 值越大,形状
    右边的阴影就扩散得越远。位移的 y 值越大,下部的阴影就越低。
     第三个参数:阴影的模糊值,以浮点值(CGFloat)来指定。指定 0.0f 将导致阴影成为固态形状。这个值越高,阴影就越
     模糊。我们很快能看到例子。
     */
    CGContextSetShadowWithColor(currentContext,CGSizeMake(10.0f, 10.0f), 20.0f,[[UIColor grayColor] CGColor]);
    /* Create the path first. Just the path handle. */
    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);
}
- (void) drawRectAtBottomOfScreen{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGMutablePathRef secondPath = CGPathCreateMutable();
    CGRect secondRect = CGRectMake(150.0f, 250.0f, 100.0f,100.0f);
    CGPathAddRect(secondPath, NULL,secondRect);
    CGContextAddPath(currentContext, secondPath);
    [[UIColor purpleColor] setFill];
    CGContextDrawPath(currentContext, kCGPathFill);
    CGPathRelease(secondPath);
}

7、位移变换、缩放变换

- (void)drawRect:(CGRect)rect{
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f,
                                  300.0f);
    //将矩形向右移动100而纵向不变
    CGAffineTransform transform = CGAffineTransformMakeTranslation(100.0f, 0.0f);
    /*//将矩形缩放0.5
    CGAffineTransform transform = CGAffineTransformMakeScale(0.5f, 0.5f);*/
    
    /* Add the rectangle to the path */
    CGPathAddRect(path,&transform, rectangle);
    /* Get the handle to the current context */
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    /* Add the path to the context */
    CGContextAddPath(currentContext,path);
    /* Set the fill color to cornflower blue */
    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
    /* Set the stroke color to brown */
    [[UIColor brownColor] setStroke];
    /* Set the line width (for the stroke) to 5 */
    CGContextSetLineWidth(currentContext,5.0f);
    /* Stroke and fill the path on the context */
    CGContextDrawPath(currentContext,kCGPathFillStroke);
    /* Dispose of the path */
    CGPathRelease(path);
}

8、旋转

- (void)drawRect:(CGRect)rect{
    /* Create the path first. Just the path handle. */
    CGMutablePathRef path = CGPathCreateMutable();
    /* Here are our rectangle boundaries */
    CGRect rectangle = CGRectMake(10.0f,10.0f, 200.0f, 300.0f);
    /* Add the rectangle to the path */
    CGPathAddRect(path,NULL, rectangle);
    /* Get the handle to the current context */
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    /* Save the state of the context to revert back to how it was at this state, later */
    CGContextSaveGState(currentContext);
    //顺时针旋转45度
    CGContextRotateCTM(currentContext,(45.0f * M_PI) / 180.0f);
    /* Add the path to the context */
    CGContextAddPath(currentContext, path);
    /* Set the fill color to cornflower blue */
    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];
    /* Set the stroke color to brown */
    [[UIColor brownColor] setStroke];
    /* Set the line width (for the stroke) to 5 */
    CGContextSetLineWidth(currentContext,5.0f);
    /* Stroke and fill the path on the context */
    CGContextDrawPath(currentContext, kCGPathFillStroke);
    /* Dispose of the path */
    CGPathRelease(path);
    /* Restore the state of the context */
    CGContextRestoreGState(currentContext);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值