xcode IOS:正确的屏幕触摸次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15885728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: correct number of touch on screen
提问by cyclingIsBetter
In my app I need to catch the exact number of finger on the screen, I try two ways but I have 2 different problem.
在我的应用程序中,我需要捕捉屏幕上手指的确切数量,我尝试了两种方法,但有 2 个不同的问题。
First way:
第一种方式:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
int count = [allTouches count];
NSLog(@"number of touch:%d", count);
}
this give me a NOT accurate number of touches if I use more finger at the same time.
如果我同时使用更多手指,这会给我不准确的触摸次数。
Second way:
第二种方式:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int num = [touches count];
totalTouch = totalTouch+num;
NSLog(@"number of touch:%d", totalTouch);
}
in this way I use a global var (totalTouch) that I increment everytime touchbegan is called and I have a perfect number of touches. Naturally i set at '0' this var in the touchend
通过这种方式,我使用了一个全局变量 (totalTouch),每次调用 touchbegan 时我都会增加它,并且我有完美的触摸次数。自然地,我在 touchend 中将此 var 设置为“0”
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
totalTouch = 0;
}
My problem is that, with the second way I do a control in touchbegan, this:
我的问题是,使用第二种方式在 touchbegan 中进行控制,这是:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int num = [touches count];
totalTouch = totalTouch+num;
if (totalTouch == numberToVerify){
//IT ENTER HERE EVERYTIME INSIDE THIS IF
}
else{
}
}
so everytime it enter inside if-conditions, and I don't want it, I want do this control only when I have a final number of touch...
所以每次它进入if条件时,我不想要它,我只想在我有最后的触摸次数时才做这个控制......
回答by codeqi
Within your
在您的
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int num = [touches count];
totalTouch = totalTouch+num;
NSLog(@"number of touch:%d", totalTouch);
}
You can get the number of fingers on the screen with
您可以使用以下命令获取屏幕上的手指数量
[[event allTouches]count]
This can be accessed from - (void)touchesBegan:
- (void)touchesEnded:
or - (void)touchesMoved:
这可以从- (void)touchesBegan:
- (void)touchesEnded:
或访问- (void)touchesMoved:
回答by rob mayoff
In touchesBegan:withEvent:
, the touches
argument only contains touches in the “begin” phase (UITouchPhaseBegan
). In touchesEnded:withEvent:
, the touches
argument only contains touches in the “end” phase (UITouchPhaseEnded
). Similarly for touchesMoved:withEvent:
and touchesCancelled:withEvent:
.
在 中touchesBegan:withEvent:
,touches
参数仅包含“开始”阶段 ( UITouchPhaseBegan
)中的触摸。在 中touchesEnded:withEvent:
,touches
参数仅包含“结束”阶段 ( UITouchPhaseEnded
)中的触摸。touchesMoved:withEvent:
和类似touchesCancelled:withEvent:
。
If you want all touches known to the system, look at event.allTouches
.
如果您希望系统知道所有触摸,请查看event.allTouches
.
If you want all touches known to the system that belong to a specific view, look at [event touchesForView:someView]
.
如果您希望系统知道属于特定视图的所有触摸,请查看[event touchesForView:someView]
。
回答by BenJuan26
The issue with your 2nd solution is that in your touchesEnded
function, you are setting totalTouch
to 0 instead of decrementing by the size of allObjects
. The function touchesEnded
does not get called when alltouches are ended; it is called when anytouches are ended.
您的第二个解决方案的问题在于,在您的touchesEnded
函数中,您设置totalTouch
为 0 而不是减少allObjects
. touchesEnded
当所有触摸都结束时,该函数不会被调用;当任何触摸结束时调用它。
If I touch once, touchesBegan
is called. If I touch again simultaneously, touchesBegan
is called again and totalTouch
is now at 2. If I lift one of the touches, touchesEnded
is called with an allObjects
length of 1, because only one touch was lifted. If we had set the counter to 0 here instead of decrementing the counter by the size of allObjects
, this would mess up the whole process. This is my solution:
如果我触摸一次,touchesBegan
则称为。如果我同时touchesBegan
再次触摸,则再次调用,totalTouch
现在为 2。如果我解除其中一个触摸,touchesEnded
则调用allObjects
长度为 1,因为只有一次触摸被解除。如果我们在此处将计数器设置为 0,而不是将计数器递减 的大小allObjects
,这会弄乱整个过程。这是我的解决方案:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
...
touchCount += touches.allObjects.count
...
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
...
touchCount -= touches.allObjects.count
...
}
回答by Hawkydoky
UPDATE For Swift 3 :
Swift 3 的更新:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
var countTouch = event?.allTouches?.count
//Do whatever you want with that
}
If you want to know if it changed at any moment, do the same in touchesMoved and put it in an array, you'll be able to analyze it.
如果您想知道它是否在任何时候发生了变化,请在 touchesMoved 中执行相同操作并将其放入数组中,您将能够对其进行分析。
Like this :
像这样 :
var countTouch:[Int] = []
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
countTouch.append((event?.allTouches?.count)!) //Not really necessary
//Do whatever you want with that
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
countTouch.append((event?.allTouches?.count)!)
}
Hope it helped someone, don't hesitate to edit or improve it. I'm a beginner in Swift, so it's totally possible there are mistakes.
希望它对某人有所帮助,不要犹豫,编辑或改进它。我是 Swift 的初学者,所以完全有可能出现错误。