我正在尝试创建一个程序,当我在与上次触摸结束的位置大致相同的位置点击时,一个圆圈会改变颜色。我尝试使用 previousLocationInView 方法,其逻辑如下:“如果 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]];
}
}