-[NSResponder swipeWithEvent:] 未调用
我正在编写一个针对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要接收 swipeWithEvent: 消息,您必须确保 3 指滑动手势未映射到任何可能导致冲突的内容。转到系统偏好设置 ->触控板 ->更多手势,并将这些首选项设置为以下选项之一:
在页面之间滑动:
在全屏应用程序之间滑动:
具体来说,全屏应用之间的滑动不应设置为三指,否则您将不会收到 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:
Swipe between full-screen apps:
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
尝试使用
[self setAcceptsTouchEvents:YES];
,其中显示// 初始化代码。
try with
[self setAcceptsTouchEvents:YES];
where it says// Initialization code here.
不确定是否是问题所在,但只有关键窗口接收手势。你的窗户有钥匙吗?
Not sure if it's the problem, but only the key window receives Gestures. Is your window key?
您的观点接受急救人员吗?
Is your view accepting first responders?