Macos 图片不移动(-setNeedsDisplay:YES不调用drawRect:)
很抱歉再次打扰您有关未调用的Macos 图片不移动(-setNeedsDisplay:YES不调用drawRect:),macos,image,drawrect,setneedsdisplay,Macos,Image,Drawrect,Setneedsdisplay,很抱歉再次打扰您有关未调用的-(void)setNeedsDisplay -(void)drawRect:方法……但我在这个问题上花了太多时间 我是Objective-C的初学者,我正在尝试做一个简单的射击。(我知道我需要工作) 但是现在,我只想在视图中提升一幅图片。例如,图片显示在视图的(0,0)处,我希望每次按下按钮时都能使图片向上(10像素) 问题是图片不动;(你们中的一些人可以看看这个吗? 代码如下: #import <Cocoa/Cocoa.h> @interface
-(void)setNeedsDisplay
-(void)drawRect:
方法……但我在这个问题上花了太多时间
我是Objective-C的初学者,我正在尝试做一个简单的射击。(我知道我需要工作)
但是现在,我只想在视图中提升一幅图片。例如,图片显示在视图的(0,0)处,我希望每次按下按钮时都能使图片向上(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
#导入
@接口maVue:NSView{
NSImageView*monmonmonstre;
国际名词;
}
@属性(读写)int nombre;
-(iAction)boutonClic:(id)发送方;
@结束
#导入“maVue.h”
@实现maVue
-(id)initWithFrame:(NSRect)帧{
self=[super initWithFrame:frame];
如果(自我){
//这里是初始化代码。
nombre=2;
monMonstre=[[NSImageView alloc]init];
}
回归自我;
}
-(void)drawRect:(NSRect)dirtyRect
{
//这里是绘图代码。
[monMonstre setFrame:CGRectMake(0,[自身名称],100100)];
[monMonstre setImage:[NSImage ImageName:@“monstre.jpg”];
[自我添加子视图:monMonstre];
}
-(iAction)boutonClic:(id)发送方
{
[self-setNombre:[self-nombre]+10];
[自我设置需要显示:是];
}
-(void)setNombre:(int)nouvaunombre
{
nombre=新的风潮;
}
-(int)名称
{
返回nombre;
}
@结束
您不需要-(无效)设置需要显示
只需对NSView
属性frame
使用standart即可
您应该重写代码,以便:
#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
#导入
@接口maVue:NSView
{
NSImageView*monmonmonstre;
国际名词;
}
@属性(读写)int nombre;
-(iAction)boutonClic:(id)发送方;
@结束
#导入“maVue.h”
@实现maVue
-(void)initWithFrame:(CGRect)frame
{
if(self=[super initWithFrame:frame])
{
nombre=2;
monMonstre=[[NSImageView alloc]init];
[monMonstre setImage:[NSImage ImageName:@“monstre.jpg”];
NSSize mSize=[monMonstre image].size;
NSRect monstreFrame;
monstreFrame=NSMakeRect(0.0f[self-nombre],mSize.width,mSize.height);
[monMonstre设置帧:monstreFrame];
[自我添加子视图:monMonstre];
[monMonstre release];//谢谢您的快速回复,使用-frame是个好主意。但是图片不再显示,因为您删除了-drawRect方法……如果我添加了-drawRect方法,当我单击NSButton时,图片会出现,但仍然不会移动……如果我尝试放置[self addSubview:monMonstre]在-boutonClic方法中,图片不会显示..初始化时只需添加一次“monMonstre”。如果要显示,应将图像设置为它)Maatthiew Maat,我已经更改了我的答案。请看initWithFrame:method。您好!我只是复制粘贴了您的代码。但是图片仍然没有移动。这太疯狂了。无论如何,谢谢您的帮助