CGContextRef 基本的知識點:
drawRect調(diào)是在Controller->loadView, Controller->viewDidLoad 兩方法之后掉用的.所以不用擔(dān)心在控制器中,這些View的drawRect就開始畫了.這樣可以在控制器中設(shè)置一些值給View(如果這些View draw的時候需要用到某些變量值).
1.如果在UIView初始化時沒有設(shè)置rect大小搪锣,將直接導(dǎo)致drawRect不被自動調(diào)用灭翔。
2.該方法在調(diào)用sizeThatFits后被調(diào)用是趴,所以可以先調(diào)用sizeToFit計算出size滑进。然后系統(tǒng)自動調(diào)用drawRect:方法泪姨。
3.通過設(shè)置contentMode屬性值為UIViewContentModeRedraw。那么將在每次設(shè)置或更改frame的時候自動調(diào)用drawRect:像樊。
4.直接調(diào)用setNeedsDisplay隘庄,或者setNeedsDisplayInRect:觸發(fā)drawRect:,但是有個前提條件是rect不能為0.
以上1,2推薦绞佩;而3,4不提倡
1寺鸥、若使用UIView繪圖猪钮,只能在drawRect:方法中獲取相應(yīng)的contextRef并繪圖。如果在其他方法中獲取將獲取到一個invalidate的ref并且不能用于畫圖胆建。drawRect:方法不能手動顯示調(diào)用烤低,必須通過調(diào)用setNeedsDisplay 或者 setNeedsDisplayInRect ,讓系統(tǒng)自動調(diào)該方法笆载。
2扑馁、若使用calayer繪圖,只能在drawInContext: 中(類似魚drawRect)繪制凉驻,或者在delegate中的相應(yīng)方法繪制腻要。同樣也是調(diào)用setNeedDisplay等間接調(diào)用以上方法。
3涝登、若要實時畫圖雄家,不能使用gestureRecognizer,只能使用touchbegan等方法來掉用setNeedsDisplay實時刷新屏幕
例子:基本的API畫出一個正方形
-(void)drawRect:(CGRect)rect{
//獲得處理的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置線條樣式
CGContextSetLineCap(context, kCGLineCapSquare);
//設(shè)置線條粗細寬度
CGContextSetLineWidth(context, 1.0);
//設(shè)置顏色
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
//開始一個起始路徑
CGContextBeginPath(context);
//起始點設(shè)置為(0,0):注意這是上下文對應(yīng)區(qū)域中的相對坐標(biāo)胀滚,
CGContextMoveToPoint(context, 0, 0);
//設(shè)置下一個坐標(biāo)點
CGContextAddLineToPoint(context, 100, 100);
//設(shè)置下一個坐標(biāo)點
CGContextAddLineToPoint(context, 0, 150);
//設(shè)置下一個坐標(biāo)點
CGContextAddLineToPoint(context, 50, 180);
//連接上面定義的坐標(biāo)點
CGContextStrokePath(context);
}
CGContextRef畫出掃一掃的區(qū)域
1趟济,畫正方形框
-(void)drawRect:(CGRect)rect{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// [self addScreenFillRect:ctx rect:self.bounds];
// [self addCenterClearRect:ctx rect:CGRectMake(100, 10, 100, 100)];
CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 0.7);
CGContextSetLineWidth(ctx, 1);
CGContextAddRect(ctx, CGRectMake(100, 100, 200, 200));
CGContextStrokePath(ctx);
}
2,畫4個角
-(void)addCenterClearRect:(CGContextRef)ctx rect:(CGRect)rect{
CGContextClearRect(ctx, rect);
}
-(void)addCorner:(CGContextRef)ctx rect:(CGRect)rect{
CGContextSetLineWidth(ctx, 2);
CGContextSetRGBStrokeColor(ctx,255 /255.0, 255/255.0, 255/255.0, 1 );
// 左上角
CGPoint leftPointsA[] = {
CGPointMake(rect.origin.x - 5, rect.origin.y + 5),
CGPointMake(rect.origin.x - 5, rect.origin.y - 5),
CGPointMake(rect.origin.x + 15, rect.origin.y - 5),
};
CGContextAddLines(ctx, leftPointsA, 3);
// 右上角
CGPoint rightPointsA[] = {
CGPointMake(rect.origin.x + rect.size.width + 5, rect.origin.y + 5),
CGPointMake(rect.origin.x + rect.size.width + 5, rect.origin.y - 5),
CGPointMake(rect.origin.x + rect.size.width -15, rect.origin.y - 5)
};
CGContextAddLines(ctx, rightPointsA, 3);
//左下角
CGPoint leftPointsB[] = {
CGPointMake(rect.origin.x - 5, rect.origin.y +rect.size.height - 5),
CGPointMake(rect.origin.x - 5, rect.origin.y + rect.size.height + 5),
CGPointMake(rect.origin.x + 15, rect.origin.y +rect.size.height + 5)
};
CGContextAddLines(ctx, leftPointsB, 3);
//右下角
CGPoint rightPointsB[] = {
CGPointMake(rect.origin.x + rect.size.width + 5, rect.origin.y +rect.size.height - 5),
CGPointMake(rect.origin.x + rect.size.width + 5, rect.origin.y + rect.size.height + 5),
CGPointMake(rect.origin.x + rect.size.width - 15, rect.origin.y +rect.size.height + 5)
};
CGContextAddLines(ctx, rightPointsB, 3);
CGContextStrokePath(ctx);
}