我有一个增加数量的按钮,当我点按它时,我会拨打服务电话。问题是,如果我多次点击它,我只想在点击它之后进行服务调用。我尝试使用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++;
}
答案 0 :(得分:4)
四种可能的解决方案
我觉得你的方法很好
- (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
或
在触发之后移除quantityChanged UITapGestureRecognizer
我的意思是,从发件人对象中删除UITapGestureRecognizer(在第一次网络服务之后的quantityChanged方法内)
然后在您收到第一个网络服务响应后,再次将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;
答案 1 :(得分:1)
即使我怀疑这会导致实用的用户界面...如果您对用户可以“放入”的最大数量有所了解,您可以选择几个依赖于每个手势的手势监听器。其他- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer
(doc here)。
代码可能如下所示:
-(void)addGestureRecognizers
{
int maxCount = 10;
UITapGestureRecognizer *other = nil;
for(int i = maxCount; i < 0; i--)
{
UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)];
tapper.numberOfTapsRequired = i;
if(other)
{
[tapper requireGestureRecognizerToFail:other];
}
[self.view addGestureRecognizer:tapper];
other = tapper;
}
}
-(void)tapped:(UITapGestureRecognizer *)tapper
{
[self callWebservice:tapper.numberOfTapsRequired];
}
我没有测试它,从未使用过它,但这就是我从文档中理解的内容。
答案 2 :(得分:0)
确定。所以我找到了解决问题的方法。我将以0.5秒的延迟执行我的Web服务调用,每次按下我的按钮时,我将取消所有等待执行的选择器并再次调用performSelector:withDelay。
答案 3 :(得分:0)
没有UITapGestureOrganizer,就更容易了。
button.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControlEvents.touchDownRepeat)
然后:
func multipleTap(_ sender: UIButton, event: UIEvent) {
let touch: UITouch = event.allTouches!.first!
if touch.tapCount == 3 {
print("triple tap")
}
}