处理Iphone上的触摸

时间:2010-07-22 06:06:31

标签: iphone objective-c cocoa-touch touchesbegan

我在处理应用程序中遇到一些问题。

我设置了这样的touchesBegan:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *currentTouch = [[event allTouches] anyObject];
    touchPoint = [currentTouch locationInView:self.view];
    if (CGRectContainsPoint(image1.frame, touchPoint)) {
        image1IsTouched = YES;
    }
}

然后我按照这样设置我的触摸动作:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    UITouch *currentTouch = [[event allTouches] anyObject];
    currentPoint = [currentTouch locationInView:currentTouch.view];

    if(image1IsTouched == YES) {
        image1.center = CGPointMake(currentPoint.x,currentPoint.y);
        .....
    }
}

现在我在实际单位上尝试了我的应用程序,这就是我注意到我的问题。当我用1个手指触摸image1时,应用程序正常运行并且每当我拖动手指时它都会检查碰撞。在触摸/拖动图像时用另一根手指触摸屏幕时会出现问题。当前正在触摸的图像将跳到另一个手指。我试过[myView setMultipleTouchEnable:NO];&在触摸时使用NSArray并将[touches count]与触摸进行比较但不起作用。有人可以告诉我如何将uiimageview设置为仅在单点触摸时动作。感谢。

1 个答案:

答案 0 :(得分:0)

首先,您应该使用UITouch *currentTouch = [touches anyObject];来获取当前触摸。

其次,您应该检查touches.count == 1以确保屏幕上只有一个手指,如果有多个手指,则忽略触摸输入,除非您想支持多点触控。