【发布时间】:2011-07-10 15:52:14
【问题描述】:
我想检测滚动手势(触控板上的两个手指)。 我该怎么做?
【问题讨论】:
-
这个documentation 应该有帮助
-
已经检查过了。没有帮助
标签: objective-c cocoa macos trackpad
我想检测滚动手势(触控板上的两个手指)。 我该怎么做?
【问题讨论】:
标签: objective-c cocoa macos trackpad
看起来您想覆盖视图的 scrollWheel: 方法。您可以使用NSEvent 的deltaX 和deltaY 方法来获取用户滚动了多少。
代码:
@implementation MyView
- (void)scrollWheel:(NSEvent *)theEvent {
NSLog(@"user scrolled %f horizontally and %f vertically", [theEvent deltaX], [theEvent deltaY]);
}
@end
您可能还想看看Handling Trackpad Events Guide。除了标准手势之外,这将向您展示如何捕获自定义手势。
【讨论】:
scrollingDeltaX 和 scrollingDeltaY 访问 NSScrollWheel 值(OX 10.7 及更高版本)。
您应该通过在您的自定义子类中实现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];
为了接收这样的事件。
【讨论】:
要检测 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)
【讨论】: