iPhone SDK 中鼠标向下和向上的手势识别器

发布于 2024-10-08 04:21:18 字数 869 浏览 0 评论 0原文

我想使用手势识别器捕获鼠标按下和鼠标松开。然而,当鼠标按下被捕获时,鼠标向上永远不会被捕获。

这就是我所做的:

首先创建一个自定义 MouseGestureRecognizer:

@implementation MouseGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    [super touchesBegan:touches withEvent:event];  
    self.state = UIGestureRecognizerStateRecognized;  
}  

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    [super touchesEnded:touches withEvent:event];  
    self.state = UIGestureRecognizerStateRecognized;  
}  
@end  

然后将识别器绑定到视图控制器中的视图:

UIGestureRecognizer *recognizer = [MouseGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
[self.view addGestureRecognizer:recognizer];  

当我在视图中单击鼠标时,将调用 TouchesBegan,但永远不会调用 TouchesEnded。是因为 UIGestureRecognizerStateRecognized 的原因吗?

I want to catch both mouse down and mouse up using gesture recognizer. However, when the mouse down is caught, mouse up is never caught.

Here's what I did:

First create a custom MouseGestureRecognizer:

@implementation MouseGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    [super touchesBegan:touches withEvent:event];  
    self.state = UIGestureRecognizerStateRecognized;  
}  

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    [super touchesEnded:touches withEvent:event];  
    self.state = UIGestureRecognizerStateRecognized;  
}  
@end  

Then bind the recognizer to a view in view controller:

UIGestureRecognizer *recognizer = [MouseGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
[self.view addGestureRecognizer:recognizer];  

When I click mouse in the view, the touchesBegan is called, but touchesEnded is never called. Is it because of the UIGestureRecognizerStateRecognized?

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

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

发布评论

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

评论(2

水波映月 2024-10-15 04:21:18

也许您可以使用 UILongPressGestureRecognizer 代替,并将 minimumPressDuration 设置为 0

Maybe you can use a UILongPressGestureRecognizer instead with minimumPressDuration set to 0.

来自 UIGestureRecognizer 类参考

运行时调用此方法(重置
手势识别器状态已
设置为 UIGestureRecognizerStateEnded
或 UIGestureRecognizerStateRecognized。 (...)在此之后
方法被调用,运行时忽略
所有剩余的活动触摸;那是,
手势识别器接收不到
进一步更新触摸
开始但尚未结束。

所以,是的,这是因为您在 touchesBegan 中将状态设置为 UIGestureRecognizerStateRecognized

编辑

作为一种解决方法,您可以创建两个识别器,一个用于 touchesBegan,另一个用于 touchesEnded,然后将它们都添加到目标看法:

UIGestureRecognizer *recognizer1 = [TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
UIGestureRecognizer *recognizer2 = [TouchUpGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:recognizer1];
[self.view addGestureRecognizer:recognizer2];

From UIGestureRecognizer class reference for reset method:

The runtime calls this method (reset) after
the gesture-recognizer state has been
set to UIGestureRecognizerStateEnded
or UIGestureRecognizerStateRecognized. (...) After this
method is called, the runtime ignores
all remaining active touches; that is,
the gesture recognizer receives no
further updates for touches that have
begun but haven't ended.

So, yes, it's because you're setting the state to UIGestureRecognizerStateRecognized in touchesBegan.

EDIT

As a workaround, you can make two recognizers, one for touchesBegan and the other for touchesEnded, and then add both of them to the target view:

UIGestureRecognizer *recognizer1 = [TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
UIGestureRecognizer *recognizer2 = [TouchUpGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:recognizer1];
[self.view addGestureRecognizer:recognizer2];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文