kolrie
kolrie

Reputation: 12732

Using @selector in RubyMotion

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?

Upvotes: 20

Views: 2317

Answers (2)

spnkr
spnkr

Reputation: 1362

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

Upvotes: 1

Dylan Markow
Dylan Markow

Reputation: 124419

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

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

Upvotes: 26

Related Questions