iOS一次检测多个触摸

时间:2014-02-19 19:04:15

标签: ios multi-touch

我正在创建一个游戏,目前如果你触摸屏幕的左半部分,一把枪顺时针旋转,如果你触摸屏幕的右半部分,它就会拍摄。

我想创建一个动作,这样如果我触摸屏幕的左半部分,它将顺时针旋转,如果你触摸右半部分,它会逆时针旋转。这很容易。

如果我同时触摸左右两半,我希望拍摄能够发生。如何同时检测2次触摸是可能的。

这是在触摸左半部分时顺时针旋转并在触摸右半部分时拍摄的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    if (touchPoint.x < self.size.width / 2.0 ) {
        isTouching = true;
    }
    else
        [self setupBullet];
}

1 个答案:

答案 0 :(得分:1)

您需要设置两个单独的对象来监听各自的触摸事件,或者您可以使用event.allTouches对象获取所有当前触摸对象的完整数组,并在您的数组中使用该数组将

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //either:
    if(touch in left side)
    {
        //Left side touch down, create object
    }
    else
    {
        //Right side touch down, create object
    }

    //Or:
    NSArray *arrayOfAllTouches = event.allTouches;
    //Now loop through that array and see what is going on on screen
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //either:
    if(touch in left side)
    {
        //Left side release, release object
    }
    else
    {
        //Right side release, release object
    }

    //Or:
    NSArray *arrayOfAllTouches = event.allTouches;
    //Now loop through that array and see what is going on on screen
}