-[NSResponder swipeWithEvent:] 未调用

发布于 2024-12-05 07:25:45 字数 1197 浏览 4 评论 0原文

我正在编写一个针对 OS X Lion 和 Snow Leopard 的应用程序。我有一个观点,我想响应滑动事件。我的理解是,如果在我的自定义视图中实现了该方法,三指滑动将调用 -[NSResponder swipeWithEvent:] 。我已经看过这个问题和相应的答案,并尝试了以下修改Oscar Del Ben 代码的存根实现:

@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

编译并运行良好,并且视图按预期呈现。 mouseDown: 事件已正确注册。但是,没有触发任何其他事件。既不是 begin/endGestureWithEvent: 方法,也不是 swipeWithEvent: 方法。这让我想知道:我是否需要在某处设置项目/应用程序设置才能正确接收和/或解释手势?预先感谢您的帮助。

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.

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

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

发布评论

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

评论(4

波浪屿的海角声 2024-12-12 07:25:45

要接收 swipeWithEvent: 消息,您必须确保 3 指滑动手势未映射到任何可能导致冲突的内容。转到系统偏好设置 ->触控板 ->更多手势,并将这些首选项设置为以下选项之一:

  • 在页面之间滑动:

    1. 用两根或三根手指滑动,或者
    2. 用三个手指滑动
  • 在全屏应用程序之间滑动:

    1. 用四根手指向左或向右滑动

具体来说,全屏应用之间的滑动不应设置为三指,否则您将不会收到 swipeWithEvent: 消息。

这两个首选项设置一起导致 swipeWithEvent: 消息发送到第一响应者。

当然,您仍然需要实现实际的滑动逻辑。如果您想像 iOS 那样执行流畅的滚动滑动,那么您将需要做更多的工作。 Lion App Kit 发行说明中的​​“流畅滑动跟踪”部分提供了如何执行此操作的示例。

请参阅http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit .html

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

风流物 2024-12-12 07:25:45

尝试使用 [self setAcceptsTouchEvents:YES]; ,其中显示 // 初始化代码。

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

爱要勇敢去追 2024-12-12 07:25:45

不确定是否是问题所在,但只有关键窗口接收手势。你的窗户有钥匙吗?

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

笔芯 2024-12-12 07:25:45

您的观点接受急救人员吗?

- (BOOL) acceptsFirstResponder
{
  return YES;
}

Is your view accepting first responders?

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