macos Mac cocoa - 如何检测触控板滚动手势?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6642058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:09:39  来源:igfitidea点击:

Mac cocoa - how can i detect trackpad scroll gestures?

objective-ccocoamacostrackpad

提问by Erik Sapir

I want to detect scroll gestures (two fingers on trackpad). How should i do that?

我想检测滚动手势(触控板上的两个手指)。我该怎么做?

回答by spudwaffle

Looks like you want to override your view's scrollWheel:method. You can use the NSEvent's deltaXand deltaYmethods to get how much the user has scrolled by.

看起来您想覆盖视图的scrollWheel:方法。您可以使用NSEvent'sdeltaXdeltaY方法来获取用户滚动了多少。

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.

您可能还想查看处理触控板事件指南。除了标准手势之外,这还将向您展示如何捕获自定义手势。

回答by spudwaffle

You should do that by implementing touch event methods of NSViewin your custom subclass. These methods are :

您应该通过NSView在您的自定义子类中实现触摸事件方法来做到这一点。这些方法是:

- (void)touchesBeganWithEvent:(NSEvent *)event;
- (void)touchesMovedWithEvent:(NSEvent *)event;
- (void)touchesEndedWithEvent:(NSEvent *)event;
- (void)touchesCancelledWithEvent:(NSEvent *)event;

The NSEventobject coming along as a paramter contains informations about the touches involded. In particular, you may retrieve them using :

NSEvent作为参数出现的对象包含有关所涉及的触摸的信息。特别是,您可以使用以下方法检索它们:

-(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.

为了接收这样的事件。

回答by AProgrammer

To detect the scrollWheel event, use - (void)scrollWheel:(NSEvent *)theEvent method.

要检测 scrollWheel 事件,请使用 - (void)scrollWheel:(NSEvent *)theEvent 方法。

    - (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,

如果您的问题是确定 scrollWheel 事件是由鼠标还是触控板生成,那么根据 Apple 的文档,这是不可能的。虽然这里有一个解决方法,

- (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 *)theEventrespectively.

您也可以使用-(void)beginGestureWithEvent:(NSEvent *)event;-(void)endGestureWithEvent:(NSEvent *)event。这些方法分别在之前和之后调用-(void)scrollWheel:(NSEvent *)theEvent

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)

在某些情况下,这将不起作用 - 如果您更快地使用两指手势并将手指从触控板中取出的速度非常快,那么您可能会在这里遇到问题 - (内存没有得到释放