2013-02-14 37 views
1

我正在嘗試學習石英的基礎知識,不明白爲什麼這不起作用 - 當按鈕被按下時,它應該將紅色矩形移動到右邊,蝙蝠在媽媽身上,它不會做任何事情 - 了「的NSLog(A」幫助「),就是揣摩,如果按鈕被運作 - 感謝在石英中移動矩形

@implementation QuartzView 

-(IBAction)moveRight:(id)sender{ 

    NSLog(@"Help"); 

    lR += 50; 

} 

- (id)initWithFrame:(NSRect)frameRect 
{ 
    self = [super initWithFrame:frameRect]; 
    if (self) { 

    } 

    return self; 
} 

- (void)drawRect:(NSRect)dirtyRect 
{ 
    CGContextRef myContext = [[NSGraphicsContext // 1 
           currentContext] graphicsPort]; 

    // Drawing code here. 
    CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);// 3 
    CGContextFillRect (myContext, CGRectMake (lR, 0, 200, 100)); 

    [self setNeedsDisplay:YES]; 
} 

@end 

感謝

+0

拍,我的意思的最後一行說「CGRectMake(LR,0,200,100)); – user2073270 2013-02-14 19:09:31

+0

修正爲你您可以編輯您的帖子,順便說一句 – Jeremy 2013-02-14 19:17:24

回答

0

嘗試[self setNeedsDisplay]內MoveRight的你增加LR後!

+1

的NSView setNeedsDisplay:。確實需要一個參數 – 2013-02-15 03:54:42

1

你不想在drawRect中調用setNeedsDisplay;簡單地說,「現在我已經提出了觀點,讓我再做一次!」

您想要在您的操作中調用setNeedsDisplay以在移動矩形後更新屏幕。

+0

好我嘗試過,但仍然無法正常工作,似乎儘管'[self setNeedsDisplay:YES];'顯示不重置....是否有任何技巧,我不知道要獲取矩形重繪? – user2073270 2013-02-20 17:01:10