我又回来了我的猪(意思是法语中的cochon)! :)
我想使用NSTimer创建一个简单的动画图像,但我找不到iphone SDK的示例。
3秒后,调试器显示:程序接收信号:“EXC_BAD_ACCESS”
这是代码,如果你有时间检查它并说出你对它的看法......
#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