ios - 如何让这些方法适用于多点触控?

标签 ios touch multi-touch

我在教程中找到了这段代码,如果触摸在屏幕的特定一侧开始,则该代码几乎将左侧或右侧的 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: 这些是 bool 值在游戏循环中所做的事情,我想要实现的目标是,如果两侧同时触摸,则 TOUCH_INCRMENT 将为 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;

最佳答案

您也许可以在没有 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,您可能会看到一些意想不到的副作用。

关于ios - 如何让这些方法适用于多点触控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14655802/

相关文章:

javascript - Google Maps API V3 双指缩放不适用于 Windows 8.1 预览版上的 IE 11

swift - 检测 UIView 上的 TouchMoved 移动的最快方法?

ios - 如何访问用户存储在iPhone中的视频或其他媒体文件?

iOS:在 ARC 下创建 ABNewPersonViewController 会给出 "Potential leak"警告

iphone - 如何在 iPhone 中停止 UIView CABasicAnimation

c# - 应用程序,提高触摸事件的性能

kinect - NUI/触摸界面的一般注意事项

ios - 使用静态 UITableViewController 创建两个部分,第一部分具有非静态数量的单元格

javascript - Safari 和录制音频 — HTML Media Capture/getUserMedia()

android - 为什么我总是从 onTouchEvent 获得 ACTION_DOWN