-[NSResponder swipeWithEvent:] not called
Asked Answered
E

4

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.

Ecru answered 15/9, 2011 at 15:7 Comment(1)
As a side note, it is possible to get scrolling gestures from -[NSResponder scrollWheel:], but this I wanted to specifically use three-fingered gestures.Ecru
A
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

Ancon answered 8/12, 2011 at 7:59 Comment(1)
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.Ecru
S
2

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

Selby answered 10/4, 2012 at 21:0 Comment(2)
I believe you meant [self setAcceptTouchEvents: YES];Uncial
Or simply override acceptsTouchEvents to return YES for your view.Similar
A
1

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

Abisia answered 15/9, 2011 at 22:11 Comment(3)
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?Ecru
Non-key windows do still receive scrollwheel events (that's why you can two-finger drag on background windows.)Abisia
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).Ecru
K
0

Is your view accepting first responders?

- (BOOL) acceptsFirstResponder
{
  return YES;
}
Kenwrick answered 15/9, 2011 at 16:54 Comment(3)
I assume you meant -(BOOL)acceptsFirstResponder; I didn't have it, but I just tried it with it and it didn't work.Ecru
Yes, I edited the answer. Are you sure you're swiping your fingers above your view?Kenwrick
Yes; I implemented other functionality that responds the scroll gesture and it works as expected, as does the mouseUp: callback.Ecru

© 2022 - 2024 — McMap. All rights reserved.