是否可以根据水龙头的位置创建水龙头?

时间:2013-05-22 00:45:23

标签: ios

我正在尝试创建一个程序,当我点击与我上次触摸结束的位置大致相同的位置时,圆圈会改变颜色。我尝试使用previousLocationInView方法使用逻辑:“if touchesbegan location == previousTouchesLocation,将颜色更改为蓝色。”此方法不起作用,因为touchesBegan和previousTouchLocation坐标具有相同的值,无论位置如何,每当我点击屏幕时总是更改颜色。如果我用touchesEnded替换previousTouchLocation,我会得到相同的结果。如果有人认为它可以提供帮助,那么这就是代码。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    touchLocation = [touch locationInView:self.view];
    lastTouchLocation = [[touches anyObject] previousLocationInView:self.view];
    distance_x = touchLocation.x - cvx;
    distance_y = cvy - touchLocation.y;
    previousDistance_x = lastTouchLocation.x - cvx;
    previousDistance_y = cvy - lastTouchLocation.y;

    if ((previousDistance_x == distance_x) && (previousDistance_y == distance_y)) {
        if (([cv.color isEqual:[UIColor redColor]])) {
           [cv setColor:[UIColor blueColor]];
        }
        else
            [cv setColor:[UIColor redColor]];
    }
}

1 个答案:

答案 0 :(得分:0)

-previousLocationInView:为您提供当前在屏幕上移动的触摸的先前位置。它不会为您提供不同的,现在已结束的触摸位置。

您需要做的是,在第一次触摸时,将触摸位置存储在属性或ivar中。然后,在第二次触摸时,将其位置与存储的位置进行比较。你需要允许一些余地 - 手指在鼠标的方式上不像素精确 - 所以移动十或二十个像素的触摸应该算作双击。

实际上,考虑到它,你最好的选择可能是使用UITapGestureRecognizer numberOfTapsRequired设置为2.这将为你处理所有的检测逻辑,你可以将它与其他手势识别器可以构建更复杂的行为。