具有委托方法的 NSTimer
我正在尝试在委托上设置一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
drawView
的方法签名很可能不正确。来自 NSTimer 类参考:因此,您的
drawView
方法应如下所示:另外,将您的代码更正为这样(注意“drawView”后面的冒号):
顺便说一句,我不确定您的
drawView
负责什么(我假设绘制一个视图)。然而,有一些内置的绘图机制,应该遵循这些机制(极少数情况除外)。通常,如果您有一个 NSView,您会调用setNeedsDisplay
,这将导致 UI 通过调用 NSView 的drawRect:
来告诉您的 NSView 重绘自身。我只是提到这一点,因为你说你是 Objective-C 的新手,所以你可能没有意识到这一点,并最终编写了比你需要的更多的代码。如果您遵循此设计,您可以让计时器定期调用setNeedsDisplay
。Most likely the method signature for
drawView
is incorrect. From the NSTimer class reference:So, your
drawView
method should look like this:Also, correct your code to be this (notice the colon following "drawView"):
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 callsetNeedsDisplay
, which will cause the UI to tell your NSView to redraw itself by calling your NSView'sdrawRect:
. 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 callsetNeedsDisplay
periodically.你说得对。
只需在方法名称中添加冒号即可,即@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.