无论我点击多少次,UITouch 点击次数都等于 1
让我先编写代码,然后解释问题
-(void)touchesBegan:(NSSet*)touches withEvent(UIEvent*)event{
1: NSUInteger numTaps=[[touches anyObject] tapCount];
2: NSString *labelText=[NSString stringWithFormat:@"Number of counts %d",
numTaps];
3: self.tapLabel.Text=labelText;
4: if (numTaps==1)
5: NSLog(@"Number of taps is 1");
}
现在,如您所见,我将 tapCount 值分配给第 1 行中的 numTaps。在第 3 行中,根据点击计数,我的标签显示计数数量 1、2、3、4。 ...n.但随后在第 4 行我检查 numTaps 是否等于 1。奇怪的事情来了。无论触摸有多少次点击,第 5 行中的命令始终会执行。所以看起来 numTaps 总是 1。但是为什么第 2 行 namTaps 并不总是 1。我确信我在那里遗漏了一些东西,但不知道是什么。
Let me first write the code and then explain the problem
-(void)touchesBegan:(NSSet*)touches withEvent(UIEvent*)event{
1: NSUInteger numTaps=[[touches anyObject] tapCount];
2: NSString *labelText=[NSString stringWithFormat:@"Number of counts %d",
numTaps];
3: self.tapLabel.Text=labelText;
4: if (numTaps==1)
5: NSLog(@"Number of taps is 1");
}
Now, as you can see I assign tapCount value to numTaps in line 1. And in line 3 depending on the tap count my label shows number of counts 1,2,3,4....n. But then in line 4 I check if numTaps equals 1. Here comes the weird stuff. No matter how many taps a touch has the command in line 5 is always executed. So it seems like numTaps is always 1. But how come in line 2 namTaps is not always one. I'm sure I'm missing something there but can't figure out what.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您可能忘记了一些;在第 4 行中有很多空格之后,因此它位于屏幕的不可见区域(至少如果您不滚动到那里)。尝试将第 5 行放在 {} 块内,如下所示
将 if 块放在大括号内始终是一个好习惯,即使里面只有一行代码。可以避免很多错误。
另一种可能性是你在 if 子句中放入了单个=。(我身上经常发生这种情况)。
这实际上将 1 分配给 numTaps 变量,因此当代码变为 1 时,其内部为 1。
From what I can see it may be, that you've forgot some ; after a lot of spaces in line 4, so it's place in invisible area of the screen(at least if you don't scroll to there). Try to put line 5 inside {} block like so
It's always good practice to put the if block inside braces, even if you have only one line of code inside. Can avoid many bugs.
Other possibility is that you've put single = in the if clause.(Happens a lot to me).
That actually assign 1 to numTaps variable so when the code goes one it is 1 inside.