DrawRect视图重绘的简要功能概述

drawRect的功能:绘制图形,图片,文字,裁剪图片
如果我们想使用drawRect方法,那么前提必须是这个类是UIView的子类,所以我们应该创建一个类继承自UIView,而drawRect就会自动的生成了,只需要将注释解开就行
在创建UIView对象的同时,会自动的调用一次这个方法
drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。
所以我们想要重新绘制这个图形时,不能手动的进行调用(没有效果),直接是用self setNeedsDisplayInRect,系统会自动的调用这个方法,进行重新的绘制

以下我只写方法,然后可以在- (void)drawRect:(CGRect)rect 方法中,进行调用即可
1.绘制普通的线条
- (void)drawLine{
   
//获取上下文
   
CGContextRef context = UIGraphicsGetCurrentContext();
   
   
//2.设置圆型
   
CGContextMoveToPoint(context, 50, 50);
   
   
//画到哪个点
   
CGContextAddLineToPoint(context, 100, 100);
   
//3.绘制,填充
   
CGContextStrokePath(context);
}
2.绘制带有属性的线
- (void)drawPropertyLine{
   
   
//获取上下文
   
CGContextRef contect =UIGraphicsGetCurrentContext();
   
//设置线条宽度
   
CGContextSetLineWidth(contect, 15);
   
//设置线条颜色
   
CGContextSetStrokeColorWithColor(contect, [[UIColor redColor] CGColor]);

   
//给自身设置圆角,也就是两条线或者多条线的四个角
//    CGContextSetLineCap(contect,kCGLineCapRound);
   
   
//给新加入的线的连接处设置圆角。
   
CGContextSetLineJoin(contect, kCGLineJoinRound);
//    新加一条线
   
CGContextMoveToPoint(contect, 50, 50);
   
CGContextAddLineToPoint(contect, 100, 100);
   
CGContextAddLineToPoint(contect, 50, 200);
   
CGContextStrokePath(contect);
   
}
效果
3.绘制四边形,矩形
-(void)drawREct{
    CGContextRef content = UIGraphicsGetCurrentContext();
   //前两个是xy的坐标,后两个是width height
    CGContextAddRect(content, CGRectMake(100, 100, 100, 100));
    CGContextStrokePath(content);
}
效果

4、设置三角形
- (void)drawRectangle{
   
   
//1.获取上下文
   
CGContextRef content = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(content, [[UIColor blueColor] CGColor]);
   
CGContextMoveToPoint(content, 100, 100);
   
CGContextAddLineToPoint(content, 100, 150);
    CGContextAddLineToPoint(content, 150, 150);
    
//  CGContextAddLineToPoint(content, 100, 100); //回到原点可以用这句话来代替
   
   
//让点自动回到起始位置
   
CGContextClosePath(content);
   
    //3.绘制,填充的是路径
    CGContextStrokePath(content);
}

5、画图
- (void)drawRound{
   
    CGContextRef contect = UIGraphicsGetCurrentContext();
    //设置线条的宽度
    CGContextSetLineWidth(contect, 20);
   //设置画图路线的颜色
    CGContextSetStrokeColorWithColor(contect, [[UIColor greenColor] CGColor]);
   
CGContextAddEllipseInRect(contect, CGRectMake(100, 200, 100, 100));
   
//重新获取图形上下文
   
CGContextRef contect2 = UIGraphicsGetCurrentContext();
   
//设置填充颜色(每个颜色的值范围都是)
    CGContextSetRGBFillColor(contect2, 0.5, 0.5, 0.5, 1);
     CGContextStrokePath(contect);
    CGContextAddEllipseInRect(contect2, CGRectMake(100, 200, 100, 100));
   
CGContextFillPath(contect2);
}


//6. 画弧
- (void)drectRadian{
   
   
CGContextRef contect = UIGraphicsGetCurrentContext();
   
//void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
   
//x,y为圆点坐标,startAngle为开始的弧度,开始弧度跟这个圆的大小有关,endAngle 结束的弧度,结束弧度,跟弧度的大小有关,clockwise 0为顺时针,1为逆时针
   
//弧度跟这个圆的大小有关
    CGContextAddArc(contect, 200, 300, 100,4,-M_PI_2, 1);
    CGContextStrokePath(contect);
}

//绘制扇形
- (void)drectFan{
   
   
CGContextRef context = UIGraphicsGetCurrentContext();
   
UIColor *aColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
   
CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
   
//10为半径围绕圆心画指定角度扇形
   
//这这句是形成扇形的关键
   
CGContextMoveToPoint(context, 200, 180);
   
CGContextAddArc(context, 200, 180, 100,  -60 * M_PI / 180, -120 * M_PI / 180, 0);
   
CGContextClosePath(context);
   
CGContextDrawPath(context, kCGPathFillStroke); //绘制路径
   
}



在UITextView中,UITextView的对象有设置一个placeHolder占位符
首先新建一个类继承自UITextView,然后解开- (void)drawRect:(CGRect)rect这个方法
@interface myTextView : UITextView

在.m中
@implementation myTextView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (
void)drawRect:(CGRect)rect {
    [
super drawRect:rect];
   
if (self.hasText)return;
   
    [
@"请输入文字" drawAtPoint:rect.origin withAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
}

然后让UIextView的光标跟着向下移动,并且让占位符的文字在检测到输入文字的时候,将文字去掉,则我们要将新建的对象,让当前的viewcontroller遵守UITextViewDelegate的协议
- (void)textViewDidChange:(UITextView *)textView{
     //想要在内容改变的时候,让UITextView进行重置,但drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。
    [textView setNeedsDisplay];
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值