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)
运行时调用此方法(
重置
)
手势识别器状态已
设置为 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];
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
也许您可以使用
UILongPressGestureRecognizer
代替,并将minimumPressDuration
设置为0
。Maybe you can use a
UILongPressGestureRecognizer
instead withminimumPressDuration
set to0
.