by shigemk2

当面は技術的なことしか書かない

ピンチで縮小と拡大イベントを発生させる(Objective-Cから移植)

ピンチインピンチアウトでビューを拡大したり縮小したりするイベントをRubyMotionで書いてみた。
書いてみようと思ったけど、RubyMotionルートで調べてもよく分からないので、
Objective-Cルートで調べてみて、それをRubyMotionに移植する方法を取った。

iPhone開発 ピンチ(二本指で操作、縮小、拡大)イベントの設定(イベント) UIPinchGestureRecognizer ios 逆引き サンプル | Linux & App Labs By pt106

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への移植はちょくちょくやっていかないといけないのかなと思った。