Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @selector in RubyMotion

Tags:

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?

like image 713
kolrie Avatar asked May 07 '12 17:05

kolrie


2 Answers

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

self.faceView.addGestureRecognizer(
  UIPinchGestureRecognizer.alloc.initWithTarget(
  self.faceView, action:'pinch'))
like image 161
Dylan Markow Avatar answered Oct 21 '22 03:10

Dylan Markow


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

like image 38
spnkr Avatar answered Oct 21 '22 04:10

spnkr