20

How do I translate the following method call from ObjectiveC to RubyMotion syntax:

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

I got this far:

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

I understand the @selector(pinch:) indicates a delegation to the receiver object pinch method, but how would I do this in RubyMotion? Maybe using a block?

kolrie
  • 12,562
  • 14
  • 64
  • 98

2 Answers2

26

You should be able to just use a string to specify the selector:

self.faceView.addGestureRecognizer(
  UIPinchGestureRecognizer.alloc.initWithTarget(
  self.faceView, action:'pinch'))
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
  • 6
    The introductory video at Pragmatic Studios covers this, and shows a couple good examples of migrating Obj-C code to ruby: http://pragmaticstudio.com/screencasts/rubymotion – Dylan Markow May 07 '12 at 17:36
  • 4
    I'm guessing your pinch action will actually look like this: 'def pinch(recognizer)' which means your selector will actually be 'pinch:' – Jaco Pretorius Feb 16 '13 at 18:09
  • 8
    As @JacoPretorius points out, if the target method takes an argument, your selector string must end with a colon `("pinch:")`. – Ben Sheldon Apr 22 '13 at 17:35
1
@gesture = UIPinchGestureRecognizer.alloc.initWithTarget(self.faceView,action:'pinch:')

self.faceView.addGestureRecognizer(@gesture)

def pinch(foo)

end

If you don't want the method handler to take an argument, use action:'pinch' instead. It will then look for a method like this:

def pinch

end

Using an instance var (@gesture = ...) is a good idea here because sometimes gesture recognizers and the GC don't play well together if you don't make the gesture var an instance var of a UIViewController. (In my experience)

spnkr
  • 952
  • 9
  • 18