问题
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?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/6747603/how-to-implement-the-two-finger-swipe-gesture-in-cocoa-to-go-back-and-forward