【问题标题】:picture does not move ( -setNeedsDisplay:YES does not call drawRect:)图片不动(-setNeedsDisplay:YES 不调用 drawRect:)
【发布时间】:2013-04-24 22:01:58
【问题描述】:

很抱歉再次打扰您关于不打电话的- (void)setNeedsDisplay - (void)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

【问题讨论】:

    标签: macos image drawrect setneedsdisplay


    【解决方案1】:

    不需要 - (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
    

    【讨论】:

    • 感谢您的快速回复Денис Пашков !使用 -frame 是个好主意。但是图片不再出现,因为您删除了-drawRect方法...如果我添加-drawRect方法,图片出现但当我单击NSButton时仍然不动...
    • 如果我尝试将 [self addSubview:monMonstre] 放在 -boutonClic 方法中,图片将不会显示..
    • 您只需要在初始化时添加一次“monMonstre”。如果要显示它,则应为其设置图像)
    • Maatthiew Maat,我改变了答案。看看 initWithFrame: 方法。
    • 你好 Денис Пашков !我只是复制粘贴您的代码..但图片仍然没有移动。这很疯狂。无论如何感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多