IOS:正确的屏幕触摸次数

标签 ios xcode touch touchesbegan

在我的应用中,我需要捕捉屏幕上手指的确切数量,我尝试了两种方法,但我遇到了 2 个不同的问题。

第一种方式:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
    int count = [allTouches count];
    NSLog(@"number of touch:%d", count);
}

如果我同时使用更多手指,这会给我一个不准确的触摸次数。

第二种方式:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    int num = [touches count];
    totalTouch = totalTouch+num;
    NSLog(@"number of touch:%d", totalTouch);
}

通过这种方式,我使用了一个全局变量 (totalTouch),每次调用 touchbegan 时我都会增加它,并且我有完美的触摸次数。 当然,我在触摸端中将此变量设置为“0”

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    totalTouch = 0;
}

我的问题是,我在 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{
    }
 }

所以每次它进入 if-conditions 时,我不想要它,我只想在我有最终的触摸次数时才做这个控制......

最佳答案

在你的

 - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     int num = [touches count];
     totalTouch = totalTouch+num;
     NSLog(@"number of touch:%d", totalTouch);
  } 

您可以通过以下方式获取屏幕上的手指数量

  [[event allTouches]count]

这可以从 - (void)touchesBegan: - (void)touchesEnded:- (void)touchesMoved: 访问p>

关于IOS:正确的屏幕触摸次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885728/

相关文章:

c++ - 如何最好地将 Xcode 和 Visual Studio 与同一个 git 存储库一起使用?

iphone - iOS sqlite 在使用 SELECT 语句时将返回 NULL

ios - xcode 7.2 APP.xctest 颜色为 "red"

objective-c - 如何检测 subview 中的点击手势

iphone - 移动物体而不实际接触物体

ios - 表格 View 更改时,单元格中的 subview 会丢失颜色

ios - dispatch_async(dispatch_get_main_queue()在 NSMutableURLRequest 超时间隔后未触发

ios - 在 Swift 2 中从 View Controller 传输数据

ios - 扩展 UITextView 并触发 onChange 事件的委托(delegate)

Blackberry Storm Emulator - TouchGesture 事件未触发,如何让 Swipe 工作?