在一段时间内更改NSImageView

时间:2014-07-17 20:41:06

标签: objective-c cocoa nsimageview

我正在尝试制作简单的掷骰子游戏。 我在过去的一年中做了大量的C编程,并且正在尝试转换为GUI和Obj C. 我的窗口内有一个按钮和两个图像单元。图像被视为输出(染料,染料2),按钮保持动作(滚筒) 我的目标是让用户单击按钮然后让图像单元格显示一些图像,然后在迭代图像更改后保留特定图像,比方说,三次。 这是我的Dice对象和我的app委托文件:

谢谢!

@implementation Dice

-(NSImage *) dicecreator {
int x;
x = rand() % 6 + 1;

    switch(x){
        case 1:
            _side = [NSImage imageNamed: @"firstside.jpg"];
            break;
        case 2:
            _side = [NSImage imageNamed: @"secondside.jpg"];
            break;
        case 3:
            _side = [NSImage imageNamed: @"thirdside.jpg"];
            break;
        case 4:
            _side = [NSImage imageNamed: @"fourthside.jpg"];
            break;
        case 5:
            _side = [NSImage imageNamed: @"fifthside.jpg"];
            break;
        case 6:
            _side = [NSImage imageNamed: @"sixthside.jpg"];
            break;
        default:
            return 0;
    }
return _side;
}
@end


@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}

- (IBAction)roller:(id)sender {

Dice *rollView = [[Dice alloc]init];

[self.dye setImage:rollView.dicecreator];

[self.dye2 setImage: rollView.dicecreator];

[self performSelector:@selector(dye) withObject:rollView.dicecreator afterDelay:0.5];

[self performSelector:@selector(dye2) withObject:rollView.dicecreator afterDelay:0.5];

[self performSelector:@selector(dye) withObject:rollView.dicecreator afterDelay:0.5];

[self performSelector:@selector(dye2) withObject:rollView.dicecreator afterDelay:0.5];

[self performSelector:@selector(dye) withObject:rollView.dicecreator afterDelay:0.5];

[self performSelector:@selector(dye2) withObject:rollView.dicecreator afterDelay:0.5];

}

@end 

1 个答案:

答案 0 :(得分:1)

你的问题是什么?

现在我可以看到调用滚子方法后大约0.5秒将调用所有选择器 - 您应该选择增加值,如0.5,1,1.5 ......

我还建议使用NSTimer类:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

编辑: 在再次阅读你的问题后,我认为你误解了选择器是什么 - 你应该在其中加入一个方法:

-(void) assignImage1:(NSImage*)image {
    [self.dye setImage:image];
}


-(void) assignImage2:(NSImage*)image {
    [self.dye2 setImage:image];
}

...

[self performSelector:@selector(assignImage1:) withObject:[rollView dicecreator] afterDelay:0.5];

另请注意,在objective-C中,您可以调用对象的函数,如:

[rollView dicecreator]