iPhone SDK 中鼠标向下和向上的手势识别器
我想使用手势识别器捕获鼠标按下和鼠标松开。然而,当鼠标按下被捕获时,鼠标向上永远不会被捕获。
这就是我所做的:
首先创建一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以使用
UILongPressGestureRecognizer
代替,并将minimumPressDuration
设置为0
。Maybe you can use a
UILongPressGestureRecognizer
instead withminimumPressDuration
set to0
.来自 UIGestureRecognizer 类参考:
所以,是的,这是因为您在
touchesBegan
中将状态设置为UIGestureRecognizerStateRecognized
。编辑
作为一种解决方法,您可以创建两个识别器,一个用于
touchesBegan
,另一个用于touchesEnded
,然后将它们都添加到目标看法:From UIGestureRecognizer class reference for
reset
method:So, yes, it's because you're setting the state to
UIGestureRecognizerStateRecognized
intouchesBegan
.EDIT
As a workaround, you can make two recognizers, one for
touchesBegan
and the other fortouchesEnded
, and then add both of them to the target view: