Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X Cocoa - how can one detect trackpad zoom gestures?

How can one detect two finger zoom gestures on MacBook trackpad? (within a selected NSView)

like image 267
hotpaw2 Avatar asked Dec 11 '22 01:12

hotpaw2


1 Answers

A simple swift solution:

var zoom:CGFloat = 0
override func magnify(with event: NSEvent) {
    super.magnify(with: event)
    if(event.phase == .changed){
        zoom += event.deltaZ
    }else if(event.phase == .began){
        zoom = 0//reset
    }else if(event.phase == .ended){
        //Swift.print("zoom: " + "\(zoom)")
        var dir:Int
        if(zoom < -100){
            Swift.print("zoom out")
            dir = 1
        }else if(zoom > 100){
            Swift.print("zoom in")
            dir = -1
        }else{
            Swift.print("no zoom")
            dir = 0
        }
    }
}

It detects if a zoom gesture has occurred +-100 deltaZ (Aka Pinch in/out to zoom)

More info and research surrounding this approach:
http://eon.codes/blog/2016/02/10/Gesture-research/

like image 96
Sentry.co Avatar answered Jan 24 '23 05:01

Sentry.co