我有一个带有两个手势识别器的 UIView。两者都用两根手指识别点击:一个用于屏幕的上半部分,另一个用于屏幕的底部。在那个 UIView 中,我有 4 个按钮覆盖整个屏幕(每个按钮是屏幕的四分之一)。
我正在使用手势识别器来检测用户何时同时按下 2 个按钮,但我仍然想识别按钮上的正常触摸。
我已经设置了一切,它工作正常。但是,当仅用一根手指按下时,按钮上的阴影会出现在 Touch Up 上,而不是在 Touch Down 上。而且感觉很奇怪。我试图改变 delaysTouchesBegan 没有成功。
有没有办法同时拥有这两种行为?用两根手指检测触摸,但是当只有一根手指时有“正常行为”?否则,我可以强制 UIbutton 的按下状态吗?
这是我设置手势的方法:
-(void)initGestureRecognition{
handClapTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handClapDetected:)];
handClapTapGestureRecognizer.numberOfTouchesRequired = 2;
handClapTapGestureRecognizer.numberOfTapsRequired = 1;
handClapTapGestureRecognizer.cancelsTouchesInView = YES;
[self.gestureRecognitionView addGestureRecognizer:handClapTapGestureRecognizer];
handClapTapGestureRecognizer.delegate = self;
jumpTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpDetected:)];
jumpTapGestureRecognizer.numberOfTouchesRequired = 2;
jumpTapGestureRecognizer.numberOfTapsRequired = 1;
jumpTapGestureRecognizer.cancelsTouchesInView = YES;
[self.gestureRecognitionView addGestureRecognizer:jumpTapGestureRecognizer];
jumpTapGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([gestureRecognizer isEqual:handClapTapGestureRecognizer] && [touch locationInView:self.view].y > self.view.frame.size.height/2)
return NO;
if ([gestureRecognizer isEqual:jumpTapGestureRecognizer] && [touch locationInView:self.view].y < self.view.frame.size.height/2)
return NO;
return YES;
}
我知道我的问题与那个类似:UIButton inside a view that has a UITapGestureRecognizer但不同之处在于,在我的情况下,行为没问题,我只是想在 Touch Down 上的按钮上获得阴影,而不是比Touch Up。
谢谢