iPhone:检测两个手指触摸 [英] iPhone: detect two finger touch

查看:150
本文介绍了iPhone:检测两个手指触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测两个手指触摸事件。如果我同时用两根手指触摸屏幕,那么就可以了。只是使用这样的代码:

I need to detect two finger touch event. If i touch the screen with two fingers on same time, so everthing is ok. Just using such code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [[touches allObjects] objectAtIndex:0];
  CGPoint point1 = [touch locationInView:self];
  CGPoint point2 ; 
  NSLog(@"First: %f %f", point1.x, point1.y) ; 
  if ([[touches allObjects] count] > 1) {
    UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
    point2 = [touch2 locationInView:self];
    NSLog(@"Second: %f %f", point2.x, point2.y) ;   
  }
}   

但如果我持有一个代码,则此代码无效用手指然后用另一根手指触摸屏幕。怎么实现这个?这很难吗?

But this code don't working if i hold one finger and then touch the screen with another finger. How to implement this ? Is it hard to do ?

推荐答案

确保UIView有 multipleTouchEnabled = YES ,默认为

Make sure the UIView has multipleTouchEnabled=YES, the default is NO.

编辑:我看到问题所在。在touchesBegan:withEvent:中,你只会得到新的触动。你没有得到所有积极的接触。如果可能的话,不太可能同时开始多次触摸。要检查是否有多个活动触摸int touchesBegan:试试这个:

I see what the problem is. In touchesBegan:withEvent:, you will only get the new touches. You don't get all the active touches. It is very unlikely, if at all possible, that you will get more than one touch to begin at the same time. To check is there is more than one active touch int touchesBegan: try this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    if ([[event touchesForView:self] count] > 1) {
        NSLog(@"%d active touches",[[event touchesForView:self.view] count]) ;


    }
    [super touchesBegan:touches withEvent:event] ;
}

这篇关于iPhone:检测两个手指触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆