I want to detect scroll gestures (two fingers on trackpad). How should i do that?
3 Answers
Looks like you want to override your view's scrollWheel:
method. You can use the NSEvent
's deltaX
and deltaY
methods to get how much the user has scrolled by.
Code:
@implementation MyView
- (void)scrollWheel:(NSEvent *)theEvent {
NSLog(@"user scrolled %f horizontally and %f vertically", [theEvent deltaX], [theEvent deltaY]);
}
@end
You also may want to take a look at the Handling Trackpad Events Guide. This will show you how to capture custom gestures, in addition to standard ones.
-
3Note that Apple’s documentation recommends using
scrollingDeltaX
andscrollingDeltaY
for accessing NSScrollWheel values (OX 10.7 and later).– DemitriMar 18, 2016 at 16:43
You should do that by implementing touch event methods of NSView
in your custom subclass.
These methods are :
- (void)touchesBeganWithEvent:(NSEvent *)event;
- (void)touchesMovedWithEvent:(NSEvent *)event;
- (void)touchesEndedWithEvent:(NSEvent *)event;
- (void)touchesCancelledWithEvent:(NSEvent *)event;
The NSEvent
object coming along as a paramter contains informations about the touches involded. In particular, you may retrieve them using :
-(NSSet *)touchesMatchingPhase:(NSTouchPhase)phase inView:(NSView *)view;
Also, in the custom view subclass, you must first set it like this :
[self setAcceptsTouchEvents:YES];
in order to recieve such events.
-
1Apple explicitly recommends against this solution. See "Mouse Events and the Trackpad" in developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…– YoavNov 10, 2013 at 10:45
-
These methods are better suited for detecting two finger swipe gestures. Use scrollWheel instead. Jan 23, 2017 at 12:06
-
Although these events can be tracked, converting the event data and keeping track of the number of touches and their positions is tricky. Since Apple provides the scrollWheel event that gives movement information and any glitching, like a touch event ending and another beginning, due to fingers bouncing, is handled, this solution is not very good at all. Apr 21 at 22:49
To detect the scrollWheel event, use - (void)scrollWheel:(NSEvent *)theEvent method.
- (void)scrollWheel:(NSEvent *)theEvent
{
//implement what you want
}
The above method will be called when you scroll using the scroll wheel from your mouse or the two finger gesture from the trackpad.
If your question is to determine if the scrollWheel event is generated by mouse or trackpad, then according to Apple's documentation, this is not possible. Although here is a work around,
- (void)scrollWheel:(NSEvent *)theEvent
{
if([theEvent phase])
{
// theEvent is generated by trackpad
}
else
{
// theEvent is generated by mouse
}
}
You might also use -(void)beginGestureWithEvent:(NSEvent *)event;
and -(void)endGestureWithEvent:(NSEvent *)event
. These methods are called before and after the -(void)scrollWheel:(NSEvent *)theEvent
respectively.
There is a case when this will not work - if you use the two finger gesture faster and take your fingers out of the trackpad pretty fast, then you might have issues here - (Memory is not getting released)
-
Refer - [link] (stackoverflow.com/questions/13807616/…). Sorry, I gave the wrong link before. Dec 11, 2012 at 18:36
-
1
-
2I tried your answer on OSX 10.10 and beginGestureWithEvent: and endGestureWithEvent: are not called when scrolling. These two methods are, according to the documentation, only activated for touch events and gestures.– HuaThamApr 1, 2015 at 0:40
-
Although the scrollWheel event has no information that tells you if the event comes from the trackpad or from the mouse (at least not for me for my Magic Mouse), you can use the touch events to keep track of touch count (don't use them for movement information) and then if the touch count is 2 at the time is is triggered, you know that the scrollWheel event must be coming from the trackpad. I tested this touch counting and it was the only solution that worked for me at the time I write this. Some code is here: blog.rectorsquid.com/… Apr 21 at 22:54