iOS重绘机制drawRect

iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,或者写一个UIVIew的子类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。
下面先说明一下绘图,比如,你想绘制一个方块,你需要写一个类来扩展UIView并在drawRect方法中填入如下代码:

- (void)drawRect:(CGRect)rect {
    //获得处理的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置线条样式
    CGContextSetLineCap(context, kCGLineCapSquare);
    //设置线条粗细宽度
    CGContextSetLineWidth(context, 1.0);
    //设置颜色
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    //开始一个起始路径
    CGContextBeginPath(context);
    //起始点设置为(0,0):注意这是上下文对应区域中的相对坐标,
    CGContextMoveToPoint(context, 0, 0);
    //设置下一个坐标点
    CGContextAddLineToPoint(context, 100, 100);
    //设置下一个坐标点
    CGContextAddLineToPoint(context, 0, 150);
    //设置下一个坐标点
    CGContextAddLineToPoint(context, 50, 180);
    //连接上面定义的坐标点
    CGContextStrokePath(context);
}

再说明一下重绘,重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)
例如:
定义一个TextField 继承于UITextField重写drawRect:方法如上,然后在ViewController去添加这么一个TextField,在初始化的时候不去设置它的frame,在touchesBegan:方法中去设置它的frame,然后调用setNeedsDisplay方法。在点击屏幕的时候,你会发现原本不出现的TextFiled重新出现在视图上

#import "ViewController.h"
#import "TextField.h"

@interface ViewController ()
@property (nonatomic,strong) TextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField = [[TextField alloc] init];
    [self.view addSubview:self.textField];
    self.view.backgroundColor = [UIColor whiteColor];
    self.textField.backgroundColor = [UIColor blueColor];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.textField.frame = CGRectMake(100, 100, 300, 300);
    [self.textField setNeedsDisplay];
}
@end

再举个自己工程中用到drawRect的例子:
还是定义一个TextField 继承于UITextField然后重写drawRect:方法如下:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
    CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5));
}

然后在ViewController中代码如下

#import "ViewController.h"
#import "TextField.h"

@interface ViewController ()
@property (nonatomic,strong) TextField *userNameTF;
@property (nonatomic,strong) TextField *userPWDTF;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.userNameTF = [[TextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    self.userNameTF.placeholder = @"请输入用户名";
    [self.view addSubview:self.userNameTF];
    
    self.userPWDTF = [[TextField alloc] initWithFrame:CGRectMake(100, 160, 200, 40)];
    self.userPWDTF.placeholder = @"请输入密码";
    [self.view addSubview:self.userPWDTF];
}
@end

效果如图:


iOS重绘机制drawRect_第1张图片
屏幕快照 2016-04-23 下午4.23.56.png

你可能感兴趣的:(iOS重绘机制drawRect)