简体   繁体   English

如何使这些方法与多点触控一起使用?

[英]How to get these methods to work with multi-touch?

I found this code on a tutorial which pretty much sets a left or right side BOOL to YES if the touch started on a specific side of the screen and then checks when the touch moves to see if it changes sides on the screen in order to make the other BOOL yes. 我在教程中找到了此代码,如果触摸在屏幕的特定侧开始,则将左侧或右侧BOOL设置为YES,然后检查触摸何时移动以查看其在屏幕的侧边是否发生了变化,以便使其他BOOL是的。

So I am now trying to implement multi-touch but I am not sure how it would work with the following code? 因此,我现在正在尝试实现多点触控,但是我不确定如何使用以下代码? Does anyone have any idea how I would go upon it? 有谁知道我会怎么做?

-(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;
}

Thanks! 谢谢!

Edit1: These are what the bools are doing in the game loop, pretty much what I am trying to achieve is that if there is a touch on both sides at the same time, the TOUCH_INCREMENT will be 0 since the touches on each side will cancel each other out. Edit1:这些是布尔值在游戏循环中的作用,我要达到的目标几乎是,如果同时在两侧进行触摸,则TOUCH_INCREMENT将为0,因为每一侧的触摸都将取消互相出来。 How would I achieve that? 我将如何实现? Anyway this is the code I am talking about: 无论如何,这就是我正在谈论的代码:

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;

You can probably make this work without the touchStartPoint (i)var. 您可能可以在没有touchStartPoint (i)var的情况下进行这项工作。 The important thing is to not use -anyObject and instead to inspect each touch. 重要的是不要使用-anyObject而是检查每次触摸。 The following code modifications might work for you: 以下代码修改可能对您有用:

-(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;
}

Here I have created a function that is called by touchesBegan and touchesMoved because they need to implement the same logic. 在这里,我创建了一个由touchesBegan和touchesMoved调用的函数,因为它们需要实现相同的逻辑。 You may see some unintended side effects if touchStartPoint is being used in the code somehow. 如果以某种方式在代码中使用touchStartPoint则可能会看到一些意外的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM