Quartz 2D简单介绍

一、什么是Quartz2D

 Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统。

我们可以使用Quartz 2D  API 来实现许多功能,如基本路径的绘制、透明度、描影、绘制阴影、透明层、颜色管理、反锯齿、PDF文档生成和PDF元数据访问。在需要的时候,Quartz 2D还可以借助图形硬件的功能。

 Quartz 2D能完成的工作:

  绘制图形 : 线条\三角形\矩形\圆\弧等

  绘制文字

  绘制\生成图片(图像)

  读取\生成PDF

  截图\裁剪图片

  自定义UI控件 

  

  涂鸦\画板

  手势解锁

二、Quartz2D在iOS开发中的价值

为了便于搭建美观的UI界面,iOS提供了UIKit框架,⾥⾯有各种各样的UI控件 

UILabel:显⽰文字
UIImageView:显示图片
UIButton:同时显示图片和⽂字(能点击)

利⽤UIKit框架提供的控件,拼拼凑凑,能搭建和现实一些简单、常见的UI界⾯

但是,有些UI界面极其复杂、⽽且⽐较个性化,⽤普通的UI控件无法实现,这时可以利用Quartz2D技术将控件内部的结构画出来,自定义控件的样子

其实,iOS中⼤部分控件的内容都是通过Quartz2D画出来的
因此,Quartz2D在iOS开发中很重要的⼀个价值是:自定义view(自定义UI控件)

 

三.Quartz2D须知

Quartz2D的API是纯C语⾔言的

Quartz2D的API来自于Core Graphics框架

需要导入CoreGraphics.framework

数据类型和函数基本都以CG作为前缀

CGContextRef
CGPathRef
CGContextStrokePath(ctx); 

四、图形上下文

图形上下文(Graphics Context):是一个CGContextRef类型的数据

图形上下文的作用:

(1)保存绘图信息、绘图状态
(2)决定绘制的输出目标(绘制到什么地⽅去?) (输出目标可以是PDF⽂文件、Bitmap或者显示器的窗口上)

相同的⼀套绘图序列,指定不同的Graphics Context,就可将相同的图像绘制到不同的目标上 


五、自定义view

如何利用Quartz2D⾃定义view?(⾃定义UI控件)

首先,得有图形上下文,因为它能保存绘图信息,并且决定着绘制到什么地方去

其次,那个图形上下⽂必须跟view相关联,才能将内容绘制到view上面

⾃定义view的步骤:

(1)新建⼀个类,继承自UIView

(2)实现-(void)drawRect:(CGRect)rect⽅法.然后在这个⽅方法中 :

1)取得跟当前view相关联的图形上下文;

2)绘制相应的图形内容

3)利用图形上下文将绘制的所有内容渲染显示到view上面 

六、补充说明

1.drawRect:

(1)为什么要实现drawRect:⽅法才能绘图到view上?

因为在drawRect:⽅法中才能取得跟view相关联的图形上下文

(2)drawRect:⽅法在什么时候被调用?

当view第一次显示到屏幕上时(被加到UIWindow上显示出来)

调用view的setNeedsDisplay或者setNeedsDisplayInRect:时 

2.drawRect:中取得的上下⽂

在drawRect:方法中取得上下文后,就可以绘制东西到view上

View内部有个layer(图层)属性,drawRect:方法中取得的是一个Layer Graphics Context,因此,绘制的东西其实是绘制到view的layer上去了

View之所以能显示东西,完全是因为它内部的layer 

七、Demo

1.饼状图Demo


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

// 执行重绘

    [self setNeedsDisplay];

}


- (void)drawRect:(CGRect)rect {

  

    NSArray *data = @[ @30, @15, @5 , @17 , @3, @10, @20];

    

//  设置圆心

    CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2);

    

//  设置半径

    CGFloat r = MIN(rect.size.width, rect.size.height) / 2;

    

//  起始弧度    结束弧度

    CGFloat start = 0;

    CGFloat end = 0;

    for (NSInteger i = 0; i <data.count; i++) {

//  结束弧度

        end = [data[i]floatValue ] / 100 * 2 * M_PI + start ;

//  创建路径

        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:r startAngle:start endAngle:end clockwise:YES];

        

//  连接圆心

        [path addLineToPoint:center];

        

//  设置饼的颜色

        [[self randomColor] set];

//   渲染

        [path fill];

        

       

        start = end;

        

    }

    

  }


// 随机饼的颜色

- (UIColor *)randomColor{

    UIColor *color = [UIColor colorWithRed:(arc4random_uniform(256)/255.0) green:(arc4random_uniform(256)/255.0) blue:(arc4random_uniform(256)/255.0) alpha:1];

    return color;


}



}


2、柱状图Demo

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

    NSArray *data = @[@300, @150.65, @55.3, @507.7, @95.8, @700, @650.65];


    CGFloat w = rect.size.width / (data.count * 2 - 1) ;

    

    for (NSInteger i = 0; i < data.count; i++) {


        CGFloat x = i *2 * w;

        

        CGFloat h = [data[i]floatValue] / 1000 * rect.size.height;

        CGFloat y = rect.size.height - h;

        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x,y, w,h)];

        

        [[self randomColor]set];

        [path fill];

        

        

    }


}


- (UIColor *)randomColor{

    UIColor *color = [UIColor colorWithRed:(arc4random_uniform(256)/255.0) green:(arc4random_uniform(256)/255.0) blue:(arc4random_uniform(256)/255.0)alpha:1];

    return color;


}


3.进度条Demo

//思路:

//在控制器中将slider的值传递给自定义view

//在自定义View中,根据传递过来的值绘制弧

//创建一个与自定义view一样大小的label来显示下载进度


#import "CZView.h"


@interface CZView ()

@property(nonatomic, strong) UILabel *progressLabel;


@end

@implementation CZView


//重写setProgress方法

- (void)setProgress:(CGFloat)progress{

    _progress = progress;

    

// 执行重绘。它不断的根据你的滑块的进度, 改变我扇形的面积,

    [self setNeedsDisplay];

//label 赋值

    self.progressLabel.text = [NSString stringWithFormat:@"已下载%.2f%%",self.progress * 100];


}


- (void)drawRect:(CGRect)rect {

    // Drawing code

    CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2);

    CGFloat r = MIN(rect.size.width, rect.size.height)/ 2;

    CGFloat start = -M_PI_2;

    CGFloat end = 2 * M_PI * self.progress + start;

    

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:r startAngle:start endAngle:end clockwise:YES];

    

    [path addLineToPoint:center];

    [[UIColor blueColor]set];

    

    [path fill];

    

}


//懒加载label

- (UILabel *)progressLabel{

    if(_progressLabel == nil){

        _progressLabel = [[UILabel alloc] init];

        _progressLabel.font = [UIFont systemFontOfSize:24.0];

        _progressLabel.textColor = [UIColor whiteColor];

        _progressLabel.textAlignment = NSTextAlignmentCenter;

        

        [self addSubview:_progressLabel];

        

    }

    return _progressLabel;

}


//设置frame

- (void)layoutSubviews{

    [super layoutSubviews];


    self.progressLabel.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);


}





转载于:https://my.oschina.net/u/2613740/blog/639526

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值