例如,我有一个视图,我想要两种不同的手势:
点击执行操作A. 双击以执行操作B.
问题在于UITapGestureRecognizer我只能设置所需的最小点击次数。单击手势识别器在双击手势识别器识别双击之前识别轻击。
示例:
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognized:)];
_tapGestureRecognizer.numberOfTouchesRequired = 1;
_tapGestureRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:_tapGestureRecognizer];
_doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGestureRecognized:)];
_doubleTapGestureRecognizer.numberOfTouchesRequired = 1;
_doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:_doubleTapGestureRecognizer];
即使我非常快速地进行双击,它也始终能识别单击。如何设置它以使轻敲手势识别器等待并看到双击手势识别器是否识别?
答案 0 :(得分:44)
这是我在一个旧项目中使用的内容,我希望它可以帮助你解决问题。
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
答案 1 :(得分:6)
在swift 3和swift 4:
let singleTap = UITapGestureRecognizer(target: self, action: #selector(doSingleTap))
singleTap.numberOfTapsRequired = 1
self.view.addGestureRecognizer(singleTap)
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doDoubleTap))
doubleTap.numberOfTapsRequired = 2
self.view.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
答案 2 :(得分:0)
@RestResource(urlMapping='/myService')
global class MyService {
@HttpGet
global static String doGet() {
return 'Hello';
}
}
在您想要的任何方法中创建手势识别器。例如,如果您想在tableView中为headerView创建轻击手势,请在
中创建这些手势 { UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTapGesture:)];
[doubleTap setDelegate:self];
doubleTap.numberOfTapsRequired = 2;
[self.headerView addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[singleTap setDelegate:self];
[doubleTap setDelaysTouchesBegan:YES];
[singleTap setDelaysTouchesBegan:YES];
[self.headerView addGestureRecognizer:singleTap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
你的班级应该实现UIGestureRecognizerDelegate。 上面提到的是该委托类的2种方法。
答案 3 :(得分:-1)
我找到了使用XCode Monkey的答案来解决单击的延迟问题。使用此方法,您可以在mili-seconds中配置单击和双击之间的阈值。这种技术使用简单的UIButton和NSTimer来区分单击和双击。
@property NSTimer *tapTimer;
@property BOOL isDoubleTap;
UIButton *yourButton = [UIButton new];
yourButton.frame = // set your frame
[yourButton addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
[self.view yourButton];
-(void)handleTap:(UIButton*)button
{
[self.tapTimer invalidate];
if (self.isDoubleTap)
{
[self doDoubleTap];
return;
}
self.isDoubleTap = YES;
// CHANGE THE 0.3 SECOND THRESHOLD BETWEEN SINGLE/DOUBLE TAP TO YOUR HEART'S CONTENT
self.tapTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(doSingleTap) userInfo:nil repeats:NO];
}
-(void)doSingleTap
{
self.isDoubleTap = NO;
}
-(void)doDoubleTap
{
self.isDoubleTap = NO;
}