ピンチインピンチアウトでビューを拡大したり縮小したりするイベントをRubyMotionで書いてみた。
書いてみようと思ったけど、RubyMotionルートで調べてもよく分からないので、
Objective-Cルートで調べてみて、それをRubyMotionに移植する方法を取った。
Objective-Cのサンプル
// Pinching in and out (for zooming a view) // UIPinchGestureRecognizer // ピンチ UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; [self.view addGestureRecognizer:pinchGesture]; // セレクター - (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender { CGFloat factor = [(UIPinchGestureRecognizer *)sender scale]; self.view.transform = CGAffineTransformMakeScale(factor, factor); NSLog(@"factor %f",factor); }
pinch_view_controller.rb
# -*- coding: utf-8 -*- class PinchViewController < UIViewController def viewDidLoad self.view.backgroundColor = UIColor.blueColor # UIPinchGestureRecognizer # pinch pinchGesture = UIPinchGestureRecognizer.alloc.initWithTarget(self, action:'handlePinchGesture:') self.view.addGestureRecognizer(pinchGesture) end # selector def handlePinchGesture(sender) factor = sender.scale self.view.transform = CGAffineTransformMakeScale(factor, factor) p factor end end
で、完成品はこんな感じ。
RubyMotionを業務ベースで使っている企業はまだまだ少ないらしいし、
伊藤直也先生が仰るように、特定の機能を実現するためにはやっぱりObjective-Cをあたらないといけないことも多いので、
Objective-CからRubyMotionへの移植はちょくちょくやっていかないといけないのかなと思った。