5

Several applications like Safari and the Finder go back and forward when you swipe with two fingers on your Magic Mouse (or with three fingers on your Magic Trackpad).

How would I implement this in my Cocoa application? What classes are available?

  • I did a quick search in the XCode developer documentation for "multitouch", and there's a whole section in the Mac OS X SnowLeopard (no space) Release Notes discussing multitouch. There's way too much to post here, but I suggest you read those notes as a starting point. – eykanal Jul 19 '11 at 13:10

2 Answers2

5

Three finger swipes are easiest, because NSResponder already does the work for you:

- (void)swipeWithEvent:(NSEvent *)event;

If you want to support two finger swipes (which I don't think technically can be classified as swipes, but rather scroll gestures), you'll have to manually process the touches- see http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW10

sbooth
  • 16,646
  • 2
  • 55
  • 81
  • This works with a two-finger swipe on the Magic Mouse. I guess you were talking about the Magic Trackpad? I don't have one yet, so I asked about the Magic Mouse. I need one to test, though. –  Jul 19 '11 at 13:49
  • You're right- I have a Magic Trackpad but no Magic Mouse, and on the Magic Trackpad two-finger swipes scroll. – sbooth Jul 19 '11 at 22:12
  • 2
    For two finger swipes on a trackpad, check out https://github.com/Kapeli/SwipableWebView. – bogdansrc Dec 23 '12 at 19:17
  • Just converted this simple obj-c solution to swift and it works great: https://github.com/oscardelben/CocoaNavigationGestures (the swift solution is apart of a bigger opensource project, but it took like 10 min to do it) – Sentry.co Oct 24 '16 at 18:52
5

For me two-finger swipe worked with the Trackpad by subclassing the NSView descendant holding the info to be swiped (e.g. a NSScrollView instance) and then to implement -(void)scrollWheel:(NSEvent *)event. This method will be invoked for two-finger swipes, the direction of the swipe can be taken from the [event deltaX] and [event deltaY] properties.

Tim
  • 1,659
  • 1
  • 21
  • 33