quartz2D图形绘制-2

 

画饼图:调用画圆的方法,添加一条直线,闭合即可生成饼状图(扇形图)

CGContextAddArc

CGContextAddLineToPoint

画曲线:添加起始点,添加曲线经过的点 设置线宽、渲染即可生成曲线

CGContextAddCurveToPoint

添加水印:先设置要添加要设置水印的图片,水印一般添加在左上角或右下角

图片裁剪:设置背景图片,并且重新绘制要显示的位置  添加一个椭圆(矩形、圆、三角形)去                  

                  裁剪图片

控件之间的相互控制:在Controller里面实例化加载一个UISlider对象,设置它的最大最小值对应于要绘制的图像的起始、终止位置,给他添加事件,在方法中带参数传值  在UIView的子类中绘制准备控制的图形 

 

 

画饼状图:

    // 先画一个圆弧,(0:顺时针 1:逆时针)添加一个直线 闭合

    CGContextRef context=UIGraphicsGetCurrentContext();

    CGContextAddArc(context, self.frame.size.width/2,     self.frame.size.height/2, 90, 0, 1, NO);//  0 1 只是真假而已

    [[self randomColor] set];

    //画圆弧之后添加一条线、闭合即可生成饼图

    CGContextAddLineToPoint(context, self.frame.size.width/2, self.frame.size.height/2 );

    CGContextClosePath(context);

    CGContextDrawPath(context, kCGPathFillStroke);

 

画曲线:

CGContextRef context=UIGraphicsGetCurrentContext();

    //曲线起点

    CGContextMoveToPoint(context, self.frame.size.width/6, self.frame.size.height/4);

    //添加曲线经过的点

    //P1->P4 经过的两个点 P5 P6 曲线的终点

    CGContextAddCurveToPoint(context, self.frame.size.width/6+80, self.frame.size.height/4-80, self.frame.size.width/6+130, self.frame.size.height/4+50,self.frame.size.width/2+100,self.frame.size.height/2-180);

    [[UIColor purpleColor] set];

    CGContextSetLineWidth(context, 15);

    //渲染

 

    CGContextStrokePath(context);

 

 

//添加水印

-(void)addShuiYin{

//获取背景图片

    UIImage* bjimage=[UIImage imageNamed:@"QQ-杨朋飞.png"];

//    P2:不缩小 P3:不透明

    UIGraphicsBeginImageContextWithOptions(bjimage.size, NO, 0);

//    重绘制背景图的放置位置

    [bjimage drawInRect:CGRectMake(0, 0, bjimage.size.width, bjimage.size.height)];

    

// 设置水印图片

    UIImage* imageIShuiYin=[UIImage imageNamed:@"1.png"];

    //设置水印图片 P1:水印的X P2:水印的Y P3:水印的w P4:水印的h

    [imageIShuiYin drawInRect:CGRectMake(bjimage.size.width-80, bjimage.size.height-80, imageIShuiYin.size.width*0.3, imageIShuiYin.size.height*0.3) blendMode:kCGBlendModeNormal alpha:0.5]; //1  不透明

 

     UIImage* imageNew=UIGraphicsGetImageFromCurrentImageContext();

    //实例化UIImageView控件

    UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

    imageView.image=imageNew;

    [self addSubview:imageView];

}

//Clip 裁剪

-(void)CaiJian{

    UIImage * image=[UIImage imageNamed:@"QQ-杨朋飞.png"];

    //获取照片尺寸     这里有背景图片的上下文

    //开始一个基于位图的图形上下文

    UIGraphicsBeginImageContext(image.size);

    

    CGContextRef context=UIGraphicsGetCurrentContext();

    //添加椭圆,用它去裁剪图片

    CGContextAddEllipseInRect(context, CGRectMake(self.frame.size.width/5, self.frame.size.height/5, image.size.width, image.size.height));

    

    CGContextClip(context);//裁剪

    [image drawAtPoint:CGPointZero];//裁剪之后重新绘制

    新的图片 上下文对象

    UIImage* imageNew=UIGraphicsGetImageFromCurrentImageContext();

 

    UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

    imageView.image=imageNew;

    imageView.backgroundColor=[UIColor yellowColor];

    [self addSubview:imageView];

    //结束一个基于位图的图形上下文

    UIGraphicsEndImageContext();

 

 

}

在Controller中实例化控件

//实例化滑块

-(void)addSlider{

    UISlider* slider=[[UISlider alloc]initWithFrame:CGRectMake(20, 300, [UIScreen mainScreen].bounds.size.width-40, 30)];

    slider.maximumValue=0;

    slider.maximumValue=6.28;

    //slider添加事件 产生方法

    [slider addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:slider];

}

//在方法里面关联它的值

-(void)change:(UISlider*)sender{

    _viewII.num=sender.value; 使滑块和绘制图形的属性相关联

//每一个View都有setNeedsDisplay 方法,会使他进入-(void)drawRect:(CGRect)rect

//setNeedsDisplay常被调用来刷新View界面

    [_viewII setNeedsDisplay];

 

 

}

num需要在view子类中定义成类属性

 

在View的子类中绘制图形

-(void)drawRect:(CGRect)rect{

 

    [self drawArc:_num];此时调用方法需要带参数

}

//画圆形

-(void)drawArc:(float)num{

    CGContextRef context=UIGraphicsGetCurrentContext();

    CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 80, 0, num, 0);num 已经和控件关联起来

    [[UIColor blueColor] set];

    [[UIColor yellowColor] setFill];

    CGContextSetLineWidth(context, 10);

    CGContextAddLineToPoint(context, self.frame.size.width/2, self.frame.size.height/2 );

    CGContextClosePath(context);

        if (num==0) {

        CGContextFillPath(context);

        }else{

        CGContextDrawPath(context, kCGPathFillStroke);

        }

}

 

 

转载于:https://my.oschina.net/131438/blog/737980

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值