3 回答

TA貢獻1876條經驗 獲得超6個贊
自定義一個UIView類,代碼如下:
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();
//設置線條樣式
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);
}
- (void)dealloc {
[super dealloc];
}
@end

TA貢獻1851條經驗 獲得超5個贊
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();
//設置線條樣式
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);
}
- (void)dealloc {
[super dealloc];
}

TA貢獻1995條經驗 獲得超2個贊
一、重繪機制
iOS的繪圖操作是在UIView類的drawRect方法中完成的,所以如果我們要想在一個UIView中繪圖,需要寫一個擴展UIView 的類,并重寫drawRect方法,在這里進行繪圖操作,程序會自動調用此方法進行繪圖。
重繪操作仍然在drawRect方法中完成,但是蘋果不建議直接調用drawRect方法,當然如果你強直直接調用此方法,當然是沒有效果的。蘋果要求我們調用UIView類中的setNeedsDisplay方法,則程序會自動調用drawRect方法進行重繪。(調用setNeedsDisplay會自動調用drawRect)。
在UIView中,重寫drawRect: (CGRect) aRect方法,可以自己定義想要畫的圖案.且此方法一般情況下只會畫一次.也就是說這個drawRect方法一般情況下只會被掉用一次. 當某些情況下想要手動重畫這個View,只需要掉用[self setNeedsDisplay]方法即可.
二、方法定義
①、- (void)drawRect:(CGRect)rect;
重寫此方法,執行重繪任務
②、- (void)setNeedsDisplay;
標記為需要重繪,異步調用drawRect
③、- (void)setNeedsDisplayInRect:(CGRect)rect;
標記為需要局部重繪
三、drawRect調用機制
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實時刷新屏幕
- 3 回答
- 0 關注
- 638 瀏覽
添加回答
舉報