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

发布于 2024-11-19 05:34:32 字数 34 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的绿洲 2024-11-26 05:34:32

看起来您想覆盖视图的 scrollWheel: 方法。您可以使用 NSEventdeltaXdeltaY 方法来获取用户滚动的距离。

代码:

@implementation MyView

- (void)scrollWheel:(NSEvent *)theEvent {
    NSLog(@"user scrolled %f horizontally and %f vertically", [theEvent deltaX], [theEvent deltaY]);
}

@end

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

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.

允世 2024-11-26 05:34:32

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

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

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

-(NSSet *)touchesMatchingPhase:(NSTouchPhase)phase inView:(NSView *)view;

另外,在自定义视图子类中,您必须首先像这样设置它:

[self setAcceptsTouchEvents:YES]; 

以便接收此类事件。

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.

眉黛浅 2024-11-26 05:34:32

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

    - (void)scrollWheel:(NSEvent *)theEvent
    {
        //implement what you want
    }

当您使用鼠标滚轮或触控板上的两根手指手势滚动时,将调用上述方法。

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

- (void)scrollWheel:(NSEvent *)theEvent
    {
        if([theEvent phase])
        {
            // theEvent is generated by trackpad
        }
        else
        {
            // theEvent is generated by mouse
        }
    }

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

在某些情况下,这不起作用 - 如果您更快地使用两根手指手势并且很快地将手指从触控板上移开,那么您可能会遇到问题 - (内存没有被释放

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文