使用macruby,如何为核心动画事务设置完成块?
我正在使用 MacRuby 进行核心动画编程。我已经尝试了所有我能想到的并进行了搜索(也许它不能在“纯”macruby中完成),但我不知道如何将MacRuby代码块指定为要调用的完成块当动画事务完成时。我知道还有其他方法可以做我想做的事情,但这对我来说似乎是最干净的,也是 Cocoa 中事物移动的方式。不管怎样,这就是我所得到的:
CATransaction.begin # start the transaction
#
# ...set up animation (works fine!)
#
CATransaction.setCompletionBlock(...) <---- Here's the problem
CATransaction.commit # end the transaction
没有“setCompletionBlock”行,动画运行正常。此 setter 方法的参数定义(在 Objective-C 中)为:
void (^)(void))block
并描述为:
“当此事务组的动画完成时调用的块对象。该块对象不带任何参数,也不返回任何值。”
我尝试过不同的事情(但我现在只是猜测):
CATransaction.setCompletionBlock({ some code })
CATransaction.setCompletionBlock(Proc.new { some code })
CATransaction.setCompletionBlock(lambda { some code })
CATransaction.setCompletionBlock(method(:aMethod))
...
def aMethod
...
end
我还差得远吗?我是否必须制作某种 Objective-C 包装器才能做到这一点?还是做不到?
提前致谢
I'm using MacRuby to do Core Animation programming. I've tried all I can think of and searched all over (and maybe it can't be done in 'pure' macruby) but I can't figure out how to specify a block of MacRuby code as a completion block to be called when an animation transaction is finished. I know there are other ways to do what I want but this seems to be the cleanest for me and the way things are moving in Cocoa. Anyway, this is what I've got:
CATransaction.begin # start the transaction
#
# ...set up animation (works fine!)
#
CATransaction.setCompletionBlock(...) <---- Here's the problem
CATransaction.commit # end the transaction
Without the 'setCompletionBlock' line the animation runs fine. The parameter to this setter method is defined (in Objective-C) as:
void (^)(void))block
And is described as:
"a block object called when animations for this transaction group are > completed. The block object takes no parameters and returns no value."
I've tried different things (but I'm just guessing at this point):
CATransaction.setCompletionBlock({ some code })
CATransaction.setCompletionBlock(Proc.new { some code })
CATransaction.setCompletionBlock(lambda { some code })
CATransaction.setCompletionBlock(method(:aMethod))
...
def aMethod
...
end
Am I way off? Do I have to make an Objective-C wrapper of some sort to do it? Or can't it be done?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,经过相当迂回的搜索分散的 MacRuby 笔记后,我找到了如何做到这一点。当然,这是我早期尝试的解决方案之一; 诀窍是安装 (MacRuby) BridgeSupport Preview,它与 MacRuby 安装是分开的,直到现在我还不知道也不需要。将其写在这里有望使某人免于寻找与问题明显无关的答案的麻烦。这是我的原始示例(上面)的“完整”列表,其中添加了缺失的部分:
其中“puts”语句可以替换为动画完成时要执行的所需代码。
为 Cocoa 方法指定块的更一般答案是使用:
Proc.new { ...代码块.. }
在方法调用中(如上所述)。如果在方法文档中指定了参数,也可以使用正常的 ruby 块定义语法来提供参数。
MacRuby BridgeSupport 预览版可以从此处下载 (MacRuby 当前版本和夜间版本也可以)。
Ok, after quite a roundabout trip searching through scattered MacRuby notes I found out how to do this. Of course it's one of my early attempted solutions; the trick was installing the (MacRuby) BridgeSupport Preview which is separate from the MacRuby install and was something I hadn't known about nor needed until now. Getting that put down here will hopefully save someone the aggravation of searching for an answer that isn't obviously connected to the problem. Here's a "complete" listing of my original example (above) with the missing piece added:
where the 'puts' statement can be replaced with desired code to be carried out on the completion of the animation.
The more general answer for specifying a block to a Cocoa method is to use:
Proc.new { ...code block... }
in the method invocation (as above). Arguments can also be supplied, if they are specified in the method documentation, by using the normal ruby block definition syntax.
The MacRuby BridgeSupport Preview can be downloaded from here (as can the MacRuby current and nightly releases).