具有委托方法的 NSTimer

发布于 2024-08-19 23:40:17 字数 334 浏览 5 评论 0原文

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

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

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

I'm trying to set up an NSTimer on a delegate - I'm so very new to objective-c so apologies if this doesn't make much sense. But what I have written is:

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

Unfortunately this just doesn't work. Can someone point me in the right direction? My mind has fried!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紅太極 2024-08-26 23:40:17

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 的 drawRect: 来告诉您的 NSView 重绘自身。我只是提到这一点,因为你说你是 Objective-C 的新手,所以你可能没有意识到这一点,并最终编写了比你需要的更多的代码。如果您遵循此设计,您可以让计时器定期调用setNeedsDisplay

Most likely the method signature for drawView is incorrect. From the NSTimer class reference:

The message to send to target when the
timer fires. The selector must have
the following signature:

- (void)timerFireMethod:(NSTimer*)theTimer

So, your drawView method should look like this:

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

Also, correct your code to be this (notice the colon following "drawView"):

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

On a side note, I'm not sure what your drawView is responsible for (I would assume drawing a view). However, there are built-in mechanisms for drawing, which should be followed (except for rare circumstances). Usually, if you have an NSView, you call setNeedsDisplay, which will cause the UI to tell your NSView to redraw itself by calling your NSView's drawRect:. I only mention this since you said you were new to Objective-C, so you might not be aware of this, and end up writing more code than you need to. If you follow this design, you could have your timer call setNeedsDisplay periodically.

冷…雨湿花 2024-08-26 23:40:17

你说得对。

只需在方法名称中添加冒号即可,即@selector(drawView:)。另外,按照惯例 Objective-C 编码器使用 YES 和 NO。

You've got it right.

Just add a colon in the method name, i.e. @selector(drawView:). Also, by convention objective-c coders use YES and NO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文