7

I am writing an application targeting OS X Lion and Snow Leopard. I have a view that I want to have respond to swipe events. My understanding is that three-finger swipes will call -[NSResponder swipeWithEvent:] if that method is implemented in my custom view. I have already looked at this question and corresponding answers, and tried the following modified stub implementation of Oscar Del Ben's code:

@implementation TestView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor redColor] set];
    NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
}

- (void)swipeWithEvent:(NSEvent *)event {
    NSLog(@"Swipe event detected!");
}

- (void)beginGestureWithEvent:(NSEvent *)event {
    NSLog(@"Gesture detected!");
}

- (void)endGestureWithEvent:(NSEvent *)event {
    NSLog(@"Gesture end detected!");
}

- (void)mouseDown:(NSEvent *)theEvent {
    NSLog(@"mouseDown event detected!");
}

@end

This compiles and runs fine, and the view renders as expected. The mouseDown: event is properly registered. However, none of the other events are triggered. Neither the begin/endGestureWithEvent: methods, nor the swipeWithEvent: method. Which makes me wonder: do I need to set a project/application setting somewhere to properly receive and/or interpret gestures? Thanks in advance for the help.

Community
  • 1
  • 1
nomothetis
  • 233
  • 2
  • 7
  • As a side note, it is possible to get scrolling gestures from `-[NSResponder scrollWheel:]`, but this I wanted to specifically use three-fingered gestures. – nomothetis Sep 15 '11 at 16:48

4 Answers4

12

To receive swipeWithEvent: messages, you have to ensure that the 3 finger swipe gesture is not mapped to anything that might cause a conflict. Go to System preferences -> Trackpad -> More Gestures, and set these preferences to one of the following:

  • Swipe between pages:

    1. Swipe with two or three fingers, or
    2. Swipe with three fingers

  • Swipe between full-screen apps:

    1. Swipe left or right with four fingers

Specifically, the swipe between full-screen apps should not be set to three fingers, otherwise you will not get swipeWithEvent: messages.

Together, these two preference settings cause swipeWithEvent: messages to be sent to the first responder.

Of course, you still have to implement the actual swipe logic. And if you want to perform a fluid scroll-swipe à la iOS, then you will need to do a little more work. There is an example of how to do this in the Lion App Kit release notes under the section "Fluid Swipe Tracking."

See http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit.html

Dalmazio
  • 1,835
  • 2
  • 23
  • 40
  • Sorry for taking to long to respond; I assumed SO would send me emails when someone responded. I use BetterTouchTool, so it's very possible that this is the reason things haven't worked as I would have liked. I'll take a look at it; meanwhile, I think this answer gives the necessary information, so I'm tagging it as correct. – nomothetis Dec 28 '11 at 19:55
2

try with [self setAcceptsTouchEvents:YES]; where it says // Initialization code here.

zpasternack
  • 17,838
  • 2
  • 63
  • 81
Jonas
  • 21
  • 2
1

Not sure if it's the problem, but only the key window receives Gestures. Is your window key?

Ken Aspeslagh
  • 11,484
  • 2
  • 36
  • 42
  • It should be; it's the only window in the application, and it receives scrolling events. Is there something that might make it not key other than me actively not setting it to be key? – nomothetis Sep 16 '11 at 13:30
  • Non-key windows do still receive scrollwheel events (that's why you can two-finger drag on background windows.) – Ken Aspeslagh Sep 16 '11 at 17:11
  • I checked; it's key. I'll see if I can create a tiny project to reproduce the behavior (this is currently happening in a moderately sized app). – nomothetis Sep 16 '11 at 20:08
0

Is your view accepting first responders?

- (BOOL) acceptsFirstResponder
{
  return YES;
}
Oscar Del Ben
  • 4,485
  • 1
  • 27
  • 41