ios Quartz2D使用(画一些简单的图形)(2)

一、画图形(这些都是要继承uiview 重写(void)drawRect:(CGRect)rect 方法,后面会说在viewcontroller中如何画)

1.圆形

  // 圆角矩形
  //   UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:100];


  // 圆弧
    // Center:圆心
    // startAngle:弧度
    // clockwise:YES:顺时针 NO:逆时针
    // 扇形
    CGPoint center = CGPointMake(125, 125);
     UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    
    // 添加一根线到圆心
    [path addLineToPoint:center];
    
    // 封闭路径,关闭路径:从路径的终点到起点
//    [path closePath];
    
    // 就是画一个圆弧
    //    [path stroke];
    
    // 填充:必须是一个完整的封闭路径,默认就会自动关闭路径
    //  画一个圆饼
    [path fill];

2.柱状图

        w = rect.size.width / (2 * arr.count - 1);
        x = 2 * w * i;
        h = [arr[i] floatValue] / 100.0 * rect.size.height;
        y = rect.size.height - h;
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];
        
        [[self colorRandom] set];
        [path fill];

3.图片和文字绘制到特定图形上面

// 超出裁剪区域的内容全部裁剪掉
    // 注意:裁剪必须放在绘制之前
//    UIRectClip(CGRectMake(0, 0, 50, 50));
    
    UIImage *image = [UIImage imageNamed:@"001"];
    
    // 默认绘制的内容尺寸跟图片尺寸一样大
//    [image drawAtPoint:CGPointZero];
    // 在哪个范围中绘制图片,就是填充 会拉伸
//    [image drawInRect:rect];
    // 平铺整个画布
    [image drawAsPatternInRect:rect];
文字
   // 绘制文字
    
    NSString *str = @"asfdsfsdf";
    // 文字的起点
    // Attributes:文本属性

    NSMutableDictionary *textDict = [NSMutableDictionary dictionary];

    // 设置文字颜色
    textDict[NSForegroundColorAttributeName] = [UIColor redColor];

    // 设置文字字体
    textDict[NSFontAttributeName] = [UIFont systemFontOfSize:30];

    // 设置文字的空心颜色和宽度

    textDict[NSStrokeWidthAttributeName] = @3;

    textDict[NSStrokeColorAttributeName] = [UIColor yellowColor];

    // 创建阴影对象
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor greenColor];
    shadow.shadowOffset = CGSizeMake(4, 4);
    shadow.shadowBlurRadius = 3;
    textDict[NSShadowAttributeName] = shadow;

    // 富文本:给普通的文字添加颜色,字体大小
    [str drawAtPoint:CGPointZero withAttributes:textDict];
    // 绘制在某个区域中,文字长会换行
//       [str drawInRect:rect withAttributes:textDict];

4.图形的一些操作

 // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.描述路径
   UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
    [[UIColor redColor] set];
    // 上下文矩阵操作
    // 注意:矩阵操作必须要在添加路径之前
    //  平移
    CGContextTranslateCTM(ctx, 100, 50);
    // 缩放
    CGContextScaleCTM(ctx, 0.5, 0.5);
    // 旋转
    CGContextRotateCTM(ctx, M_PI_4);
    // 3.把路径添加上下文
    CGContextAddPath(ctx, path.CGPath);
    [[UIColor redColor] set];
    
    
    // 4.渲染上下文
    CGContextFillPath(ctx);

demo地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 810评论 0 3
  • 本人写这个纯粹是为了记住ios中繁琐的动画,一个各种各样的路径,上年纪了,记不住了。 绘图的步骤: 1.获取上下文...
    摄影师诺风阅读 494评论 0 0
  • 什么是Quartz2D 是一个二维的绘图引擎,同时支持iOS和Mac系统 Quartz2D的API是纯C语言的,它...
    Mario_ZJ阅读 611评论 0 1
  • 在布达拉宫山下,时不时能见到朝圣的藏人。他们每走两步都要双手合一,举过头顶,然后跪下,双手落地,再向前滑动,...
    我是大王_48f5阅读 377评论 0 1
  • 作为iOS开发,XCode一天编译几十上百遍是常事。cmd+B,两指一按,哗啦啦的就开始build了。但是,我们是...
    微微笑的蜗牛阅读 6,590评论 1 51