我有一个增加数量的按钮,当我点击它时,我会拨打服务电话。问题是,如果我多次点击它,我只想在完成点击之后再拨打服务电话。我尝试使用UITapGestureRecognizer,但不知道如何计算抽头数。
任何帮助,将不胜感激。
这是示例代码
- (void)quantityChanged:(UITapGestureRecognizer *)tapRecognizer {
static NSUInteger numberOfTaps;
if (tapRecognizer.state == UIGestureRecognizerStateBegan) {
numberOfTaps = 0;
}
if (tapRecognizer.state == UIGestureRecognizerStateEnded)
NSLog(@"%d",numberOfTaps);
if (tapRecognizer.state == UIGestureRecognizerStateChanged)
numberOfTaps++;
}
最佳答案
四种可能的解决方案
我觉得你的方法很好
- (void)quantityChanged:(UITapGestureRecognizer *)tapRecognizer {
static NSUInteger numberOfTaps;
if (tapRecognizer.state == UIGestureRecognizerStateEnded) {
numberOfTaps ++;
NSLog(@"web service call with %d",numberOfTaps);
}
}
查看日志
web service call with 1
web service call with 2
web service call with 3
web service call with 4
要么
触发触发后删除数量更改的UITapGestureRecognizer
我的意思是从发件人对象中删除UITapGestureRecognizer(在您的第一个Web服务之后的quantityChanged方法内)
在您收到第一个Web服务响应后,再将UITapGestureRecognizer添加到发件人。
要么
使用UILongPressGestureRecognizer。这是一个漫长的事件过程。请检查事件状态的开始,完成等,并编写代码。
- (IBAction)longPressDetected:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
NSLog(@"Long press began");
} else if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized ) {
NSLog(@"Long press UIGestureRecognizerStateRecognized");
}
else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
}
else {
NSLog(@"Long press detected.");
}
}
要么
设置UITapGestureRecognizer的numberOfTapsRequired
UITapGestureRecognizer *doubleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(beginComicTransitions:)] autorelease];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
doubleTapRecognizer.delegate = self;
关于iphone - 使用UITapGestureRecognizer计数点击次数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11965163/