2012-11-22 14 views
0
- (void)drawRect:(CGRect)rect 
{ 
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f}; 
CGContextSetStrokeColor(c, red); 
CGContextBeginPath(c); 
CGContextMoveToPoint(c, 150.0f, 200.0f); 
CGContextSetLineWidth(c, 5.0f); 
CGContextAddLineToPoint(c, 50,300); 
CGContextAddLineToPoint(c, 260,300); 
CGContextClosePath(c); 
} 

這裏是我的viewwillappear的iOS無法畫出簡單的線條

-(void)viewWillAppear:(BOOL)animated 
{ 
    [self.view setNeedsDisplay];   
} 

然而,鑑於無法劃清界線,只是白色的空白。

+0

你的代碼沒有意義。什麼是'c'?如果這是你的真實代碼,編譯器會抱怨。無論如何,或者你正在以一種無意義的方式將'c'設置成一個伊娃。您只能從'drawRect'中獲取繪圖上下文,並且您的代碼沒有這樣做。所以問題不在於iOS無法繪製;這就是*你*不知道如何畫畫。我的書教你:http://www.apeth.com/iOSBook/ch15.html – matt

+0

c = uigraphicgetccurrentcontext –

+0

但這不在你的代碼中。那麼你的代碼只是一個謊言? – matt

回答

1

你試過這個嗎?

- (void)drawRect:(CGRect)rect 
    [super drawRect:rect]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 

    // Draw them with a 2.0 stroke width so they are a bit more visible. 
    CGContextSetLineWidth(context, 2.0); 


    CGContextMoveToPoint(context, 0,0); //start at this point 

    CGContextAddLineToPoint(context, 20, 20); //draw to this point 

    // and now draw the Path! 
    CGContextStrokePath(context); 
} 
+0

'uiviewcontroller'不能超級拖拉 –

1

看來你正在使用的UIViewController子類drawRect方法。但drawRect方法僅適用於UIView子類。

如果我是對的,那麼請執行以下操作。否則seee @Anoop Vaidya的回答。這應該是有效的。

子類的UIView,做你的東西在drawRect並添加視圖到您的視圖控制器。

@interface myView : UIView 
{ 

} 
@end 

@implementation myView 

- (void)drawRect:(CGRect)rect{ 

} 

@end 

    -(void)viewWillAppear:(BOOL)animated 
{ 
    myView *view1 = [[myView alloc]init]; 
    self.view = view1;   
} 
1

你可以使用這個drawRect Tutorial這對於開始非常有用。關於你的問題,確保你正在繪製的UIView是可見的視圖,也許你重寫了UIView,或者在這個視圖的頂部有其他視圖,並且因爲它不可能看到這些變化。也可能在顯示之前忘記初始化視圖。