在RubyMotion中使用@selector

问题描述 投票:20回答:2

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

[self.faceView addGestureRecognizer:[
    [UIPinchGestureRecognizer alloc] initWithTarget:self.faceView
    action:@selector(pinch:)]];

我到目前为止:

self.faceView.addGestureRecognizer(
  UIPinchGestureRecognizer.alloc.initWithTarget(
  self.faceView, action:???))

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

objective-c syntax rubymotion
2个回答
26
投票

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

self.faceView.addGestureRecognizer(
  UIPinchGestureRecognizer.alloc.initWithTarget(
  self.faceView, action:'pinch'))

1
投票
@gesture = UIPinchGestureRecognizer.alloc.initWithTarget(self.faceView,action:'pinch:')

self.faceView.addGestureRecognizer(@gesture)

def pinch(foo)

end

如果您不希望方法处理程序接受参数,请改用action:'pinch'。然后它会寻找这样的方法:

def pinch

end

使用实例var(@gesture = ...)在这里是一个好主意,因为如果你没有使手势var成为UIViewController的实例变量,有时手势识别器和GC不能很好地协同工作。 (在我的经验中)

© www.soinside.com 2019 - 2024. All rights reserved.