How to implement the two-finger swipe gesture in Cocoa to go back and forward?

断了今生、忘了曾经 提交于 2019-12-21 17:07:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!