3 回答

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
自定義一個(gè)UIView類(lèi),代碼如下:
MainView.h
#import <UIKit/UIKit.h>
@interface MainView : UIView {
}
@end
MainView.m
#import "MainView.h"
@implementation MainView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
self.backgroundColor=[UIColor cyanColor];
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
//獲得處理的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置線條樣式
CGContextSetLineCap(context, kCGLineCapSquare);
//設(shè)置線條粗細(xì)寬度
CGContextSetLineWidth(context, 1.0);
//設(shè)置顏色
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
//開(kāi)始一個(gè)起始路徑
CGContextBeginPath(context);
//起始點(diǎn)設(shè)置為(0,0):注意這是上下文對(duì)應(yīng)區(qū)域中的相對(duì)坐標(biāo),
CGContextMoveToPoint(context, 0, 0);
//設(shè)置下一個(gè)坐標(biāo)點(diǎn)
CGContextAddLineToPoint(context, 100, 100);
//設(shè)置下一個(gè)坐標(biāo)點(diǎn)
CGContextAddLineToPoint(context, 0, 150);
//設(shè)置下一個(gè)坐標(biāo)點(diǎn)
CGContextAddLineToPoint(context, 50, 180);
//連接上面定義的坐標(biāo)點(diǎn)
CGContextStrokePath(context);
}
- (void)dealloc {
[super dealloc];
}
@end

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
MainView.h
#import <UIKit/UIKit.h>
@interface MainView : UIView {
}
@end
MainView.m
#import "MainView.h"
@implementation MainView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
self.backgroundColor=[UIColor cyanColor];
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
//獲得處理的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置線條樣式
CGContextSetLineCap(context, kCGLineCapSquare);
//設(shè)置線條粗細(xì)寬度
CGContextSetLineWidth(context, 1.0);
//設(shè)置顏色
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
//開(kāi)始一個(gè)起始路徑
CGContextBeginPath(context);
//起始點(diǎn)設(shè)置為(0,0):注意這是上下文對(duì)應(yīng)區(qū)域中的相對(duì)坐標(biāo),
CGContextMoveToPoint(context, 0, 0);
//設(shè)置下一個(gè)坐標(biāo)點(diǎn)
CGContextAddLineToPoint(context, 100, 100);
//設(shè)置下一個(gè)坐標(biāo)點(diǎn)
CGContextAddLineToPoint(context, 0, 150);
//設(shè)置下一個(gè)坐標(biāo)點(diǎn)
CGContextAddLineToPoint(context, 50, 180);
//連接上面定義的坐標(biāo)點(diǎn)
CGContextStrokePath(context);
}
- (void)dealloc {
[super dealloc];
}

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
一、重繪機(jī)制
iOS的繪圖操作是在UIView類(lèi)的drawRect方法中完成的,所以如果我們要想在一個(gè)UIView中繪圖,需要寫(xiě)一個(gè)擴(kuò)展UIView 的類(lèi),并重寫(xiě)drawRect方法,在這里進(jìn)行繪圖操作,程序會(huì)自動(dòng)調(diào)用此方法進(jìn)行繪圖。
重繪操作仍然在drawRect方法中完成,但是蘋(píng)果不建議直接調(diào)用drawRect方法,當(dāng)然如果你強(qiáng)直直接調(diào)用此方法,當(dāng)然是沒(méi)有效果的。蘋(píng)果要求我們調(diào)用UIView類(lèi)中的setNeedsDisplay方法,則程序會(huì)自動(dòng)調(diào)用drawRect方法進(jìn)行重繪。(調(diào)用setNeedsDisplay會(huì)自動(dòng)調(diào)用drawRect)。
在UIView中,重寫(xiě)drawRect: (CGRect) aRect方法,可以自己定義想要畫(huà)的圖案.且此方法一般情況下只會(huì)畫(huà)一次.也就是說(shuō)這個(gè)drawRect方法一般情況下只會(huì)被掉用一次. 當(dāng)某些情況下想要手動(dòng)重畫(huà)這個(gè)View,只需要掉用[self setNeedsDisplay]方法即可.
二、方法定義
①、- (void)drawRect:(CGRect)rect;
重寫(xiě)此方法,執(zhí)行重繪任務(wù)
②、- (void)setNeedsDisplay;
標(biāo)記為需要重繪,異步調(diào)用drawRect
③、- (void)setNeedsDisplayInRect:(CGRect)rect;
標(biāo)記為需要局部重繪
三、drawRect調(diào)用機(jī)制
drawRect調(diào)用是在Controller->loadView,,Controller->viewDidLoad 兩方法之后調(diào)用的。所以不用擔(dān)心在控制器中,這些View的drawRect就開(kāi)始畫(huà)了。這樣可以在控制器中設(shè)置一些值給View(如果這些View draw的時(shí)候需要用到某些變量值).
1、如果在UIView初始化時(shí)沒(méi)有設(shè)置rect大小,將直接導(dǎo)致drawRect不被自動(dòng)調(diào)用。
2、該方法在調(diào)用sizeThatFits后被調(diào)用,所以可以先調(diào)用sizeToFit計(jì)算出size。然后系統(tǒng)自動(dòng)調(diào)用drawRect:方法。
3、通過(guò)設(shè)置contentMode屬性值為UIViewContentModeRedraw。那么將在每次設(shè)置或更改frame的時(shí)候自動(dòng)調(diào)用drawRect:。
4、直接調(diào)用setNeedsDisplay,或者setNeedsDisplayInRect:觸發(fā)drawRect:,但是有個(gè)前提條件是rect不能為0.
以上1,2推薦;而3,4不提倡
四、繪圖
1、若使用UIView繪圖,只能在drawRect:方法中獲取相應(yīng)的contextRef并繪圖。如果在其他方法中獲取將獲取到一個(gè)invalidate的ref并且不能用于畫(huà)圖。drawRect:方法不能手動(dòng)顯示調(diào)用,必須通過(guò)調(diào)用setNeedsDisplay 或者 setNeedsDisplayInRect ,讓系統(tǒng)自動(dòng)調(diào)該方法。
2、若使用calayer繪圖,只能在drawInContext: 中(類(lèi)似于drawRect)繪制,或者在delegate中的相應(yīng)方法繪制。同樣也是調(diào)用setNeedDisplay等間接調(diào)用以上方法。
3、若要實(shí)時(shí)畫(huà)圖,不能使用gestureRecognizer,只能使用touchbegan等方法來(lái)掉用setNeedsDisplay實(shí)時(shí)刷新屏幕
添加回答
舉報(bào)