Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-[NSResponder swipeWithEvent:] not called

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.

like image 565
nomothetis Avatar asked Sep 15 '11 15:09

nomothetis


2 Answers

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

like image 172
Dalmazio Avatar answered Nov 08 '22 14:11

Dalmazio


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

like image 21
Jonas Avatar answered Nov 08 '22 14:11

Jonas