iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。
下面先说明一下绘图,比如,你想绘制一个方块,你需要写一个类来扩展UIView并在drawRect方法中填入如下代码:
- (void)drawRect:(CGRect)rect { <wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">// Drawing code. </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//获得处理的上下文 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextRef context = UIGraphicsGetCurrentCont</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">ext(); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置线条样式 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetLineCap(context, kCGLineCapSquare); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置线条粗细宽度 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetLineWidth(context, 1.0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置颜色 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetRGBStrokeCol</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">or(context, 1.0, 0.0, 0.0, 1.0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//开始一个起始路径 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextBeginPath(context); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标, </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextMoveToPoint(context, 0, 0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 100, 100); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 0, 150); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 50, 180); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//连接上面定义的坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextStrokePath(context); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">} </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">再说明一下重绘,重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">在UIView中,重写drawRect: (CGRect) aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次.</span><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
2.该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3.通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4.直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.
以上1,2推荐;而3,4不提倡
1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext: 中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。
3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕
iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。下面先说明一下绘图,比如,你想绘制一个方块,你需要写一个类来扩展UIView并在drawRect方法中填入如下代码:
- (void)drawRect:(CGRect)rect { <wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">// Drawing code. </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//获得处理的上下文 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextRef context = UIGraphicsGetCurrentCont</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">ext(); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置线条样式 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetLineCap(context, kCGLineCapSquare); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置线条粗细宽度 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetLineWidth(context, 1.0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置颜色 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetRGBStrokeCol</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">or(context, 1.0, 0.0, 0.0, 1.0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//开始一个起始路径 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextBeginPath(context); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标, </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextMoveToPoint(context, 0, 0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 100, 100); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 0, 150); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 50, 180); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//连接上面定义的坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextStrokePath(context); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">} </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">再说明一下重绘,重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">在UIView中,重写drawRect: (CGRect) aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次.</span><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
drawRect调是在Controller->loadView, Controller->viewDidLoad 两方法之后掉用的.所以不用担心在控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量值).
1.如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。2.该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3.通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4.直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.
以上1,2推荐;而3,4不提倡
1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext: 中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。
3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕
下面先说明一下绘图,比如,你想绘制一个方块,你需要写一个类来扩展UIView并在drawRect方法中填入如下代码:
- (void)drawRect:(CGRect)rect { <wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">// Drawing code. </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//获得处理的上下文 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextRef context = UIGraphicsGetCurrentCont</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">ext(); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置线条样式 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetLineCap(context, kCGLineCapSquare); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置线条粗细宽度 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetLineWidth(context, 1.0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置颜色 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextSetRGBStrokeCol</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">or(context, 1.0, 0.0, 0.0, 1.0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//开始一个起始路径 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextBeginPath(context); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标, </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextMoveToPoint(context, 0, 0); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 100, 100); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 0, 150); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//设置下一个坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextAddLineToPoint(context, 50, 180); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">//连接上面定义的坐标点 </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">CGContextStrokePath(context); </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left"></span><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">} </span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">再说明一下重绘,重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">在UIView中,重写drawRect: (CGRect) aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次.</span><span style="font-family:Arial; font-size:14px; color:#333333; line-height:26px; text-align:left">当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
drawRect调是在Controller->loadView, Controller->viewDidLoad 两方法之后掉用的.所以不用担心在控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量值).
1.如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。2.该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3.通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4.直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.
以上1,2推荐;而3,4不提倡
1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext: 中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。
3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕