3

我正在尝试在代表上设置一个 NSTimer - 我对 Objective-c 非常陌生,所以如果这没有多大意义,我深表歉意。但是我写的是:

animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self.delegate selector:@selector(drawView) userInfo:nil repeats:TRUE];

不幸的是,这不起作用。有人可以指出我正确的方向吗?我的心已经炸了!!

4

2 回答 2

4

最有可能的方法签名drawView不正确。从 NSTimer 类参考:

计时器触发时发送给目标的消息。选择器必须具有以下签名:

- (void)timerFireMethod:(NSTimer*)theTimer

因此,您的drawView方法应如下所示:

- (void)drawView:(NSTimer*)theTimer
{
// Draw the view
}

此外,将您的代码更正为(注意“drawView”后面的冒号):

animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self.delegate selector:@selector(drawView:) userInfo:nil repeats:TRUE];

在旁注中,我不确定您drawView负责什么(我假设绘制视图)。但是,有内置的绘图机制,应该遵循(除了极少数情况)。通常,如果你有一个 NSView,你调用setNeedsDisplay,这将导致 UI 告诉你的 NSView 通过调用你的 NSView 来重绘自己drawRect:。我只提到这一点,因为你说你是 Objective-C 的新手,所以你可能没有意识到这一点,最终编写的代码比你需要的多。如果您遵循此设计,您可以setNeedsDisplay定期调用计时器。

于 2010-01-29T17:09:14.757 回答
3

你做对了。

只需在方法名称中添加一个冒号,即@selector(drawView:)。此外,按照惯例,objective-c 编码器使用 YES 和 NO。

于 2010-01-29T17:11:37.550 回答