2

我正在使用 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 包装器才能做到这一点?还是做不到?

提前致谢

4

1 回答 1

2

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:

CATransaction.begin  # start the transaction
#
# ...set up animation (works fine!)
#
CATransaction.setCompletionBlock( Proc.new { puts "I'm done!" })      <-------
CATransaction.commit  # end the transaction

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).

于 2011-05-02T10:04:14.313 回答