0

我又带着我的猪回来了(在法语中是 cochon 的意思)!:)

我想用 NSTimer 创建一个简单的动画图像,但我找不到不使用 iphone SDK 的示例。

3秒后,调试器显示:程序接收信号:“EXC_BAD_ACCESS”</p>

这是代码,如果您有时间检查它并说出您的想法..

#import <Cocoa/Cocoa.h>


@interface maatView : NSView {
    NSArray *mesImagesCochon;
    NSImageView * monCochon;
    int counter;
}
-(void)tick;

@end

这是 .m 文件:

#import "maatView.h"


@implementation maatView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
            }
    return self;
}

- (void)awakeFromNib
{
    counter=0;
    mesImagesCochon = [NSArray arrayWithObjects:
                       [NSImage imageNamed:@"cochon.png"],
                       [NSImage imageNamed:@"cochon2.png"],
                       [NSImage imageNamed:@"cochon3.png"],
                       [NSImage imageNamed:@"cochon4.png"],
                       nil];

    [NSTimer scheduledTimerWithTimeInterval:3 
        target:self 
        selector:@selector(tick)
            userInfo:nil
            repeats:NO];

    monCochon = [[NSImageView alloc]init];
    [monCochon setFrame:NSMakeRect(0, 0, 100, 100)];

}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
}

-(void)tick
{
    NSLog(@"method appele %d",mesImagesCochon.count);
    counter++;
    if (counter > mesImagesCochon.count) 
    {
        counter = 0;
    }

    [monCochon setImage:[mesImagesCochon objectAtIndex:counter]];
    [self addSubview:monCochon];
}
@end
4

0 回答 0