【问题标题】:Mac cocoa - how can i detect trackpad scroll gestures?Mac cocoa - 我如何检测触控板滚动手势?
【发布时间】:2011-07-10 15:52:14
【问题描述】:

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

【问题讨论】:

  • 这个documentation 应该有帮助
  • 已经检查过了。没有帮助

标签: objective-c cocoa macos trackpad


【解决方案1】:

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

代码:

@implementation MyView

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

@end

您可能还想看看Handling Trackpad Events Guide。除了标准手势之外,这将向您展示如何捕获自定义手势。

【讨论】:

  • 请注意,Apple 的文档建议使用 scrollingDeltaXscrollingDeltaY 访问 NSScrollWheel 值(OX 10.7 及更高版本)。
【解决方案2】:

您应该通过在您的自定义子类中实现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]; 

为了接收这样的事件。

【讨论】:

【解决方案3】:

要检测 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 之前和之后调用。

在某些情况下这不起作用 - 如果您更快地使用两指手势并且非常快地将手指从触控板上移出,那么您可能会遇到问题 - (Memory is not getting released)

【讨论】:

  • 参考 - [链接] (stackoverflow.com/questions/13807616/…)。抱歉,我之前给错了链接。
  • 不正确。使用 Apple Magic Mouse 滚动时,相位可能 > 0。
  • 我在 OSX 10.10 上尝试了你的答案,并且 beginGestureWithEvent: 和 endGestureWithEvent: 在滚动时被 not 调用。根据文档,这两种方法仅针对触摸事件和手势激活。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多