我正在编写一个针对 OS X Lion 和 Snow Leopard 的应用程序。我有一个观点,我想响应滑动事件。我的理解是三指滑动会调用-[NSResponder swipeWithEvent:]
如果该方法在我的自定义视图中实现。我已经看过了this https://stackoverflow.com/questions/7421687/voidswipewitheventnsevent-event-not-working-on-lion问题和相应的答案,并尝试了 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:
方法。这让我想知道:我是否需要在某处设置项目/应用程序设置才能正确接收和/或解释手势?先谢谢您的帮助。
受到滑动事件:消息时,您必须确保 3 指滑动手势未映射到任何可能导致冲突的内容。转到系统首选项 -> 触控板 -> 更多手势,并将这些首选项设置为以下选项之一:
-
在页面之间滑动:
- 用两根或三根手指滑动,或者
- 用三个手指滑动
-
在全屏应用程序之间滑动:
- 用四根手指向左或向右滑动
具体来说,全屏应用之间的滑动不应该设置为三指,否则你将无法得到滑动事件:消息。
这两个首选项设置共同导致滑动事件:要发送给第一响应者的消息。
当然,您仍然需要实现实际的滑动逻辑。如果您想像 iOS 那样执行流畅的滚动滑动,那么您将需要做更多的工作。 Lion App Kit 发行说明中的“流畅滑动跟踪”部分提供了如何执行此操作的示例。
See http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit.html http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)