DrawRect视图重绘的简要功能概述
来源:互联网 发布:磁条读写器软件 编辑:程序博客网 时间:2024/06/16 04:46
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);
//获取上下文
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, [[UIColorredColor]CGColor]);
//给自身设置圆角,也就是两条线或者多条线的四个角
// CGContextSetLineCap(contect,kCGLineCapRound);
//给新加入的线的连接处设置圆角。
CGContextSetLineJoin(contect,kCGLineJoinRound);
// 新加一条线
CGContextMoveToPoint(contect,50,50);
CGContextAddLineToPoint(contect,100,100);
CGContextAddLineToPoint(contect,50,200);
CGContextStrokePath(contect);
//获取上下文
CGContextRef contect =UIGraphicsGetCurrentContext();
//设置线条宽度
CGContextSetLineWidth(contect,15);
//设置线条颜色
CGContextSetStrokeColorWithColor(contect, [[UIColorredColor]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();
//前两个是x、y的坐标,后两个是width,height、
CGContextAddRect(content,CGRectMake(100,100,100,100));
CGContextStrokePath(content);
}
效果
4、设置三角形
- (void)drawRectangle{
//1.获取上下文
CGContextRef content = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(content, [[UIColorblueColor]CGColor]);
CGContextMoveToPoint(content,100,100);
CGContextAddLineToPoint(content,100,150);
//1.获取上下文
CGContextRef content = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(content, [[UIColorblueColor]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, [[UIColorgreenColor]CGColor]);
CGContextAddEllipseInRect(contect,CGRectMake(100,200,100,100));
//重新获取图形上下文
CGContextRef contect2 = UIGraphicsGetCurrentContext();
//设置填充颜色(每个颜色的值范围都是)
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);
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为逆时针
//弧度跟这个圆的大小有关
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 = [UIColorcolorWithRed:0green:1blue:1alpha: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);//绘制路径
CGContextRef context =UIGraphicsGetCurrentContext();
UIColor *aColor = [UIColorcolorWithRed:0green:1blue:1alpha: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 {
[superdrawRect:rect];
if (self.hasText)return;
[@"请输入文字"drawAtPoint:rect.originwithAttributes:@{NSForegroundColorAttributeName:[UIColorgreenColor]}];
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
[superdrawRect:rect];
if (self.hasText)return;
[@"请输入文字"drawAtPoint:rect.originwithAttributes:@{NSForegroundColorAttributeName:[UIColorgreenColor]}];
}
然后让UIextView的光标跟着向下移动,并且让占位符的文字在检测到输入文字的时候,将文字去掉,则我们要将新建的对象,让当前的viewcontroller遵守UITextViewDelegate的协议
- (void)textViewDidChange:(UITextView *)textView{
//想要在内容改变的时候,让UITextView进行重置,但drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。
[textViewsetNeedsDisplay];
}
@end
1 0
- DrawRect视图重绘的简要功能概述
- 重绘机制drawRect
- IOS drawRect 重绘
- Phone重绘机制drawRect
- Phone重绘机制drawRect
- Phone重绘机制drawRect
- 关于view重绘drawRect
- iPhone重绘机制drawRect
- Phone重绘机制drawRect
- Phone重绘机制drawRect
- iPhone重绘机制drawRect
- Phone重绘机制drawRect
- iPhone重绘机制drawRect
- iOS重绘机制drawRect
- Phone重绘机制drawRect
- iPhone重绘机制drawRect
- iPhone重绘机制drawRect
- iPhone重绘机制drawRect
- SIGCHLD信号
- CloseHandle()函数的使用
- 2.3 Bean装配
- java中this的使用方法
- Java反射机制常用方法介绍
- DrawRect视图重绘的简要功能概述
- C++对象模型
- 【vijos】P1335 数独验证
- 用C++实现一个自动注册的工厂
- 登录遇见的小问题
- window下彻底卸载删除SQL Serever 2012
- 获取了职业资格证书二级/技师
- hbase 伪分布环境搭建
- c++primer(第五版) 第十章 泛型算法习题答案