2013-04-24 31 views
0

對不起,又來打擾你有關- (void)setNeedsDisplay不調用 - (void)drawRect:方法...但我花那麼多時間在這個問題上畫面不動(-setNeedsDisplay:是不調用的drawRect :)

我我是Objective-C的初學者,我正試圖做一個簡單的拍攝。 (我知道我需要工作)

但現在,我只是想在視圖中提出一張照片。例如,圖片出現在視圖的(0,0)處,我希望這樣可以使圖片每次按下NSButton時(10像素)。

問題是畫面不動;(一些你可以看看這個 這裏是代碼:

#import <Cocoa/Cocoa.h> 


@interface maVue : NSView { 

    NSImageView * monMonstre; 
    int nombre; 
} 
@property (readwrite) int nombre; 

- (IBAction)boutonClic:(id)sender; 

@end 








#import "maVue.h" 


@implementation maVue 


- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
     nombre = 2; 
     monMonstre = [[NSImageView alloc]init]; 
    } 
    return self; 
} 


- (void)drawRect:(NSRect)dirtyRect 
{ 
    // Drawing code here. 
    [monMonstre setFrame:CGRectMake(0,[self nombre],100,100)]; 
    [monMonstre setImage:[NSImage imageNamed:@"monstre.jpg"]]; 
    [self addSubview:monMonstre]; 
} 


- (IBAction)boutonClic:(id)sender 
{ 
    [self setNombre:[self nombre]+10]; 
    [self setNeedsDisplay:YES]; 

} 


- (void)setNombre:(int)nouveauNombre 
{ 
    nombre=nouveauNombre; 
} 

- (int)nombre 
{ 
    return nombre; 
} 
@end 

回答

0

不需要- (void)setNeedsDisplay

而已!使用標準爲NSView屬性frame

您應該重寫您的代碼:

#import <Cocoa/Cocoa.h> 

@interface maVue : NSView 
{ 
    NSImageView * monMonstre; 
    int nombre; 
} 
@property (readwrite) int nombre; 

- (IBAction)boutonClic:(id)sender; 

@end 


#import "maVue.h" 

@implementation maVue 

- (void)initWithFrame:(CGRect)frame 
{ 
    if(self = [super initWithFrame:frame]) 
    { 
    nombre = 2; 
    monMonstre = [[NSImageView alloc] init]; 
    [monMonstre setImage:[NSImage imageNamed:@"monstre.jpg"]]; 
    NSSize mSize = [monMonstre image].size; 
    NSRect monstreFrame; 
    monstreFrame = NSMakeRect(0.0f, [self nombre], mSize.width, mSize.height); 
    [monMonstre setFrame:monstreFrame]; 
    [self addSubview:monMonstre]; 
    [monMonstre release]; // <-- only if you don't use ARC (Automatic Reference Counting) 
    } 
    return self; 
} 

- (IBAction)boutonClic:(id)sender 
{ 
    [self setNombre:[self nombre]+10]; 

    NSRect frame = [monMonstre frame]; 
    frame.origin.y = [self nombre]; 

    [monMonstre setFrame:frame] 
} 

- (void)setNombre:(int)nouveauNombre 
{ 
    nombre=nouveauNombre; 
} 

- (int)nombre 
{ 
    return nombre; 
} 

@end 
+0

感謝您的快速回復ДенисПашков!使用-frame是個好主意。但圖片不再出現,因爲你刪除了-drawRect方法...如果我添加-drawRect方法,圖片出現,但仍然不動,當我點擊NSButton ... – 2013-04-24 22:40:17

+0

,如果我嘗試把一個[self addSubview:monMonstre]在-boutonClic方法中,圖片不會顯示。 – 2013-04-24 22:46:14

+0

您只需在初始化時添加'monMonstre'一次。如果你希望它被顯示,你應該設置圖像) – 2013-04-25 00:01:42

相關問題