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

时间:2010-12-16 20:31:11

标签: iphone ios objective-c uigesturerecognizer

我想使用手势识别器捕捉鼠标和鼠标。但是,当捕获鼠标时,鼠标悬停永远不会被捕获。

这就是我的所作所为:

首先创建一个自定义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?

2 个答案:

答案 0 :(得分:4)

也许您可以使用UILongPressGestureRecognizer,而minimumPressDuration设置为0

答案 1 :(得分:2)

来自reset reset方法:

  

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

所以,是的,这是因为您在touchesBegan中将状态设置为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]; ,然后将它们添加到目标视图中:

{{1}}