【iOS开发】Quartz2D的简单使用

来源:互联网 发布:数据可视化公司招聘 编辑:程序博客网 时间:2024/06/05 00:18
转载自:http://blog.csdn.net/ttf1993/article/details/45030771

画直线

    //拿到当前画布    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.拼接图形(路径)    // 设置线段宽度    CGContextSetLineWidth(ctx, 10);    // 设置线段头尾部的样式    CGContextSetLineCap(ctx, kCGLineCapRound);    // 设置线段转折点的样式    CGContextSetLineJoin(ctx, kCGLineJoinRound);    // 设置颜色    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);    // 设置一个起点    CGContextMoveToPoint(ctx, 10, 10);    // 添加一条线段到(100, 100)    CGContextAddLineToPoint(ctx, 100, 100);    // 渲染一次    CGContextStrokePath(ctx);

画三角形

    // 1.获得画布    CGContextRef ctx = UIGraphicsGetCurrentContext();      // 2.画三角形    CGContextMoveToPoint(ctx, 0, 0);    CGContextAddLineToPoint(ctx, 100, 100);    CGContextAddLineToPoint(ctx, 150, 80);    // 关闭路径(连接起点和最后一个点)    CGContextClosePath(ctx);    //设置颜色    CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);    // 3.绘制图形    CGContextStrokePath(ctx);

画矩形

   // 1.获得画布  CGContextRef ctx = UIGraphicsGetCurrentContext();  // 2.画矩形  CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));  // set : 同时设置为实心和空心颜色  // setStroke : 设置空心颜色  // setFill : 设置实心颜色  [[UIColor whiteColor] set];//CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);  // 3.绘制图形  CGContextFillPath(ctx);

画圆

  // 1.获得画布    CGContextRef ctx = UIGraphicsGetCurrentContext();     // 2.画圆    CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 100, 100));      CGContextSetLineWidth(ctx, 10);    // 3.显示所绘制的东西    CGContextStrokePath(ctx);

画圆弧

// 1.获得画布    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.画圆弧    // x\y : 圆心    // radius : 半径    // startAngle : 开始角度    // endAngle : 结束角度    // clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);    // 3.显示所绘制的东西    CGContextFillPath(ctx);

画图片

UIImage *image = [UIImage imageNamed:@"me"];    // 2.画图片//    [image drawAtPoint:CGPointMake(50, 50)];//    [image drawInRect:CGRectMake(0, 0, 150, 150)];    [image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];

画字符串

   // 1.获得画布    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.画矩形    CGRect cubeRect = CGRectMake(50, 50, 100, 100);    CGContextAddRect(ctx, cubeRect);    // 3.显示所绘制的东西    CGContextFillPath(ctx);    // 4.画文字    NSString *str = @"哈哈哈哈Good morning hello hi hi hi hi";    //    [str drawAtPoint:CGPointZero withAttributes:nil];    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];    // NSForegroundColorAttributeName : 文字颜色    // NSFontAttributeName : 字体    attrs[NSForegroundColorAttributeName] = [UIColor redColor];    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:50];    [str drawInRect:cubeRect withAttributes:attrs];
0 0