0

我在一个教程中找到了这段代码,如果触摸开始于屏幕的特定一侧,则几乎将左侧或右侧 BOOL 设置为 YES,然后检查触摸何时移动以查看它是否在屏幕上改变侧面以使另一个 BOOL 是的。

所以我现在正在尝试实现多点触控,但我不确定它如何与以下代码一起使用?有谁知道我会怎么做?

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    touchStartPoint = [touch locationInView:self.view];
    if (touchStartPoint.x < 160.0) {
        touchLeftDown = TRUE;
    }
    else if (touchStartPoint.x > 160.0) {
        touchRightDown = TRUE;
    } 
}

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

    if (touchStartPoint.x < 160.0 && currentTouchPoint.x < 160.0) {
        touchLeftDown = TRUE;
    }
    else if (touchStartPoint.x > 160.0 && currentTouchPoint.x > 160.0)
    {
        touchRightDown = TRUE;
    }
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchLeftDown = touchRightDown = FALSE;
}

谢谢!

编辑1:这些是布尔在游戏循环中所做的事情,我几乎想要实现的是,如果双方同时触摸,TOUCH_INCREMENT 将为 0,因为每一侧的触摸都会取消互相出去。我将如何实现这一目标?无论如何,这是我正在谈论的代码:

if (touchLeftDown == TRUE) {
        touchStep -= TOUCH_INCREMENT;
    }
    else if (touchRightDown == TRUE) {
        touchStep += TOUCH_INCREMENT;
    }
    else {
        touchStep = SLOWDOWN_FACTOR * touchStep;
    }
    touchStep = MIN(MAX(touchStep, -MAX_ABS_X_STEP), MAX_ABS_X_STEP);
    pos.x += touchStep;
4

1 回答 1

1

您可能可以在没有touchStartPoint(i)var 的情况下完成这项工作。重要的是不要使用-anyObject,而是检查每次触摸。以下代码修改可能对您有用:

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

    int leftTouches=0;
    int rightTouches=0;

    for (UITouch *touch in touches) 
    { 
        CGPoint location = [touch locationInView:touch.view];
        //just in case some code uses touchStartPoint
        touchStartPoint=location;

        if (location.x < 160.0) {
            leftTouches++;
        }
        else if (location.x > 160.0) {
            rightTouches++;
        }
    }

    //reset touch state
    touchLeftDown=FALSE;

    //set touch state if touch found
    if(leftTouches>0){
        touchLeftDown=TRUE;
    }

    touchRightDown=FALSE;
    if(rightTouches>0){
        touchRightDown=TRUE;
    }

}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self countTouches:touches withEvent:event];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self countTouches:touches withEvent:event];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchLeftDown = touchRightDown = FALSE;
}

在这里,我创建了一个由 touchesBegan 和 touchesMoved 调用的函数,因为它们需要实现相同的逻辑。touchStartPoint如果以某种方式在代码中使用,您可能会看到一些意想不到的副作用。

于 2013-02-02T07:59:39.730 回答