LoginSignup
2
2

More than 5 years have passed since last update.

NSMagnificationGestureRecognizer  ワークアラウンド

Last updated at Posted at 2015-12-24

NSMagnificationGestureRecognizer

NSMagnificationGestureRecognizer は OS X の Gesture Recognizer でトラックパッドなどを二本の指でつまんだジェスチャーでズームなどに利用できる。iOS の経験者であれば、UIPinchGestureRecognizer の親戚かと直感的に思うかもしれない。

UIPinchGestureRecognizer であれば、view の transform を scale プロパティでスケールしてやればいい。

// viewController 
func scaleGesture(gesture: UIPinchGestureRecognizer) {
    view.transform = CGAffineTransformScale(self.view.transform, gesture.scale, gesture.scale); 
}

NSMagnificationGestureRecognizer で同様なコードを書くと不思議な現象が発生する。なんと、この magnification は 拡大は正、縮小は負とするとんでもない値をプロパティに持つ。確かにドキュメントを見ると、1.0 を加えれば scale factor になるよと書いてある。

Declaration
var magnification: CGFloat
Discussion
This property contains the current magnification in effect for the gesture. Add the value of this property to 1.0 to get the scale factor to apply to your content.

よって、1.0の場合は、2倍ズームで、2の場合は3倍ズームであるのはいいのだが、問題は縮小(ズームアウト)させたい時だ、半分に縮小させる時は よせばいいのに -1.0 が帰ってくるというわけだ。これでは、まともに CGAffineTransformScale とかにそのまま渡すと、上下左右が反転してします。当然、負の値を渡しているからだ…

なので、magnification を scale factor として使いたい場合は

let magnification = gesture.magnification
let scaleFactor = (magnification >= 0.0) ? (1.0 + magnification) : 1.0 / (1.0 - magnification)

これで、-1.00.5-2.0(三分の一だよ) は 0.3333という具合に、scale factor として利用できるようになります。

NSMagnificationGestureRecognizer は UIPinchGestureRecognizer の後にできたのだから、改善されているならともかく、改悪されているのはいかがなものか、せめて、UIPinchGesture と同様に scale factor として利用できるようにして欲しかった。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2