├── MyClass.h ├── MyClass.m └── README /MyClass.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | @interface MyClass 4 | 5 | @property (strong) NSMutableDictionary *twoFingersTouches; 6 | 7 | @end -------------------------------------------------------------------------------- /MyClass.m: -------------------------------------------------------------------------------- 1 | #import "MyClass.h" 2 | 3 | #define kSwipeMinimumLength 0.2 4 | 5 | @implementation MyClass 6 | 7 | - (void)touchesBeganWithEvent:(NSEvent *)event{ 8 | if(event.type == NSEventTypeGesture){ 9 | NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:self]; 10 | if(touches.count == 2){ 11 | self.twoFingersTouches = [[NSMutableDictionary alloc] init]; 12 | 13 | for (NSTouch *touch in touches) { 14 | [self.twoFingersTouches setObject:touch forKey:touch.identity]; 15 | } 16 | } 17 | } 18 | } 19 | 20 | 21 | - (void)touchesMovedWithEvent:(NSEvent*)event { 22 | NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseEnded inView:self]; 23 | if(touches.count > 0){ 24 | NSMutableDictionary *beginTouches = [self.twoFingersTouches copy]; 25 | self.twoFingersTouches = nil; 26 | 27 | NSMutableArray *magnitudes = [[NSMutableArray alloc] init]; 28 | 29 | for (NSTouch *touch in touches) 30 | { 31 | NSTouch *beginTouch = [beginTouches objectForKey:touch.identity]; 32 | 33 | if (!beginTouch) continue; 34 | 35 | float magnitude = touch.normalizedPosition.x - beginTouch.normalizedPosition.x; 36 | [magnitudes addObject:[NSNumber numberWithFloat:magnitude]]; 37 | } 38 | 39 | float sum = 0; 40 | 41 | for (NSNumber *magnitude in magnitudes) 42 | sum += [magnitude floatValue]; 43 | 44 | // See if absolute sum is long enough to be considered a complete gesture 45 | float absoluteSum = fabsf(sum); 46 | 47 | if (absoluteSum < kSwipeMinimumLength) return; 48 | 49 | // Handle the actual swipe 50 | // This might need to be > (i am using flipped coordinates) 51 | if (sum > 0){ 52 | NSLog(@"go back"); 53 | }else{ 54 | NSLog(@"go forward"); 55 | } 56 | } 57 | } 58 | 59 | @end -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This Code shows how to use scroll and swipe gestures in Leopard and Lion. 2 | 3 | In Lion, the user can choose to enable two or three fingers gestures for navigation, as well as enabling natural scrolling. In Leopard, user can only use swipe gestures (unless you choose to do otherwise) 4 | 5 | The code should be self explanatory. It's up to you to implement the goBack and goForward methods. --------------------------------------------------------------------------------