当用户将手指按到屏幕上时,我想执行连续动作。这目前工作正常。但是当用户在屏幕上按下第二根手指时,我希望UILongPressGestureRecognizer取消/结束第一次按下并开始服从新手指。这样做的最佳方法是什么?我需要有两个UILongPressGestureRecognizers
吗?当一个火灾将另一个设置为
enabled=NO
;启用= YES;
还是有更清洁的方式?
目前,仅使用一个UILongPressGestureRecognizer
,当第二根手指按到屏幕上时,它的行为就像它甚至不知道它在那里一样。
答案 0 :(得分:3)
UILongPressGestureRecognizer
只知道最小触摸次数 - 默认情况下,一根手指 - 然后放下另一根手指不会产生太大影响。在需要单次触摸的真实手机上尝试此操作会看到初始触摸导致识别器转换到状态1.无论是向侧面移动第一根手指还是向下触摸第二根手指,都会导致转换到状态2.抬起第一根手指触发导致转换到状态3并结束手势。
我将第二个UILongPressGestureRecognizer
添加到与上面第一个相同的视图中,但是给了这一个最小的双触点要求。像这样:
UILongPressGestureRecognizer *lpgr1 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgr1Method:)];
UILongPressGestureRecognizer *lpgr2 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgr2Method:)];
lpgr2.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:lpgr1];
[self.view addGestureRecognizer:lpgr2];
// ...
- (void)lpgr1Method:(UIGestureRecognizer *)gestureRecognizer {
UIGestureRecognizerState state = gestureRecognizer.state;
self.stateLabel.text = [NSString stringWithFormat:@"%d", (int)state];
}
- (void)lpgr2Method:(UIGestureRecognizer *)gestureRecognizer {
UIGestureRecognizerState state = gestureRecognizer.state;
self.stateLabel2.text = [NSString stringWithFormat:@"%d", (int)state];
}
如果我先用一根手指触摸并握住它,lpgr1
将进入状态1.如果我再用第二根手指触摸并按住它,lpgr1
将进入状态2. {{ 1}}根本不会发射。
如果我一次用两根手指触地并按住它,lpgr2
会发射,lpgr2
永远不会发射。
所以看起来在整个视图中添加两个识别器只会令人困惑,无论你如何编程它都无法实现你想要的结果。因此,正确的方法是编写自己的lpgr1
子类,我怀疑。
编辑:我也试过了:
UIGestureRecognizer
这确实允许两个识别器同时触发,并识别出第二根手指已经触及。但是,只要第一根手指被移除,lpgr2.delegate = self;
lpgr1.delegate = self;
// ...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return true;
}
就会结束。我也无法做到你想做的事情。我会编写子类,但我需要注意其他地方。祝你好运!
答案 1 :(得分:-1)
我刚刚制作了一个新的手势识别器类。它很简单,适用于1或2次触摸的连续动作。
FCTwoTouchesRecognizer.h
#import <UIKit/UIKit.h>
@interface FCTwoTouchesRecognizer : UIGestureRecognizer
@end
FCTwoTouchesRecognizer.m
#import "FCTwoTouchesRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation FCTwoTouchesRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if ([touches count] > 2) //only 1 or 2 touches
{
self.state = UIGestureRecognizerStateFailed;
return;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed) return;
self.state = UIGestureRecognizerStateChanged;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if ( (self.numberOfTouches - [touches count]) == 0 )
self.state = UIGestureRecognizerStateRecognized;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
self.state = UIGestureRecognizerStateFailed;
}
@end
用法:
1)添加
FCTwoTouchesRecognizer *recog = [[FCTwoTouchesRecognizer alloc] initWithTarget:self action:@selector(twoTouchesGestureHandler:)];
[self addGestureRecognizer:recog];
2)处理
-(void)twoTouchesGestureHandler:(FCTwoTouchesRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateChanged)
{
NSLog(@"changed");
if ([recognizer numberOfTouches] > 1)
{
CGPoint point1 = [recognizer locationOfTouch:0 inView:self];
CGPoint point2 = [recognizer locationOfTouch:1 inView:self];
}
else
{
CGPoint point = [recognizer locationInView:self];
}
}
if(recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"touches ended");
}
}