【问题标题】:iOS detecting multiple touches at onceiOS一次检测多个触摸
【发布时间】:2014-02-19 19:04:15
【问题描述】:

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

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

如果我同时触摸左半边和右半边,我希望射击发生。这怎么可能同时检测到 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];
}

【问题讨论】:

  • [触及所有对象] ?

标签: ios multi-touch


【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多