在RubyMotion中使用@selector

kol*_*rie 20 syntax objective-c rubymotion

如何将以下方法调用从ObjectiveC转换为RubyMotion语法:

[self.faceView addGestureRecognizer:[
    [UIPinchGestureRecognizer alloc] initWithTarget:self.faceView
    action:@selector(pinch:)]];
Run Code Online (Sandbox Code Playgroud)

我到目前为止:

self.faceView.addGestureRecognizer(
  UIPinchGestureRecognizer.alloc.initWithTarget(
  self.faceView, action:???))
Run Code Online (Sandbox Code Playgroud)

我理解@selector(pinch:)指示接收器对象pinch方法的委托,但我如何在RubyMotion中执行此操作?也许用块?

Dyl*_*kow 26

您应该只能使用字符串来指定选择器:

self.faceView.addGestureRecognizer(
  UIPinchGestureRecognizer.alloc.initWithTarget(
  self.faceView, action:'pinch'))
Run Code Online (Sandbox Code Playgroud)

  • 正如@JacoPretorius指出的那样,如果目标方法接受一个参数,你的选择器字符串必须以冒号`("pinch:")`结尾. (8认同)
  • Pragmatic Studios的介绍视频介绍了这一点,并展示了将Obj-C代码迁移到ruby的几个很好的例子:http://pragmaticstudio.com/screencasts/rubymotion (6认同)
  • 我猜你的捏动作实际上看起来像这样:'def pinch(识别器)'这意味着你的选择器实际上是'捏:' (4认同)