Mac cocoa - 如何检测触控板滚动手势?
我想检测滚动手势(触控板上的两个手指)。 我该怎么做?
I want to detect scroll gestures (two fingers on trackpad).
How should i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想检测滚动手势(触控板上的两个手指)。 我该怎么做?
I want to detect scroll gestures (two fingers on trackpad).
How should i do that?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
看起来您想覆盖视图的
scrollWheel:
方法。您可以使用NSEvent
的deltaX
和deltaY
方法来获取用户滚动的距离。代码:
您可能还想看看 处理触控板事件指南。除了标准手势之外,这将向您展示如何捕获自定义手势。
Looks like you want to override your view's
scrollWheel:
method. You can use theNSEvent
'sdeltaX
anddeltaY
methods to get how much the user has scrolled by.Code:
You also may want to take a look at the Handling Trackpad Events Guide. This will show you how to capture custom gestures, in addition to standard ones.
您应该通过在自定义子类中实现
NSView
的触摸事件方法来实现这一点。这些方法是:
作为参数出现的 NSEvent 对象包含有关所涉及触摸的信息。特别是,您可以使用以下方式检索它们:
另外,在自定义视图子类中,您必须首先像这样设置它:
以便接收此类事件。
You should do that by implementing touch event methods of
NSView
in your custom subclass.These methods are :
The
NSEvent
object coming along as a paramter contains informations about the touches involded. In particular, you may retrieve them using :Also, in the custom view subclass, you must first set it like this :
in order to recieve such events.
要检测scrollWheel事件,请使用 - (void)scrollWheel:(NSEvent *)theEvent 方法。
当您使用鼠标滚轮或触控板上的两根手指手势滚动时,将调用上述方法。
如果你的问题是确定scrollWheel事件是由鼠标还是触控板生成的,那么根据Apple的文档,这是不可能的。尽管这里有一个解决方法,但
您也可以使用
-(void)beginGestureWithEvent:(NSEvent *)event;
和-(void)endGestureWithEvent:(NSEvent *)event
。这些方法分别在-(void)scrollWheel:(NSEvent *)theEvent
之前和之后调用。在某些情况下,这不起作用 - 如果您更快地使用两根手指手势并且很快地将手指从触控板上移开,那么您可能会遇到问题 - (内存没有被释放)
To detect the scrollWheel event, use - (void)scrollWheel:(NSEvent *)theEvent method.
The above method will be called when you scroll using the scroll wheel from your mouse or the two finger gesture from the trackpad.
If your question is to determine if the scrollWheel event is generated by mouse or trackpad, then according to Apple's documentation, this is not possible. Although here is a work around,
You might also use
-(void)beginGestureWithEvent:(NSEvent *)event;
and-(void)endGestureWithEvent:(NSEvent *)event
. These methods are called before and after the-(void)scrollWheel:(NSEvent *)theEvent
respectively.There is a case when this will not work - if you use the two finger gesture faster and take your fingers out of the trackpad pretty fast, then you might have issues here - (Memory is not getting released)