Objective c 限制移动到特定对象的触摸

Objective c 限制移动到特定对象的触摸,objective-c,Objective C,我有8块瓷砖,以3x3的网格排列,有一个空白点。然而,唯一可以移动的瓦片是在空白点旁边的瓦片。 我有一个中心坐标方法,可以获取您触摸的任何瓷砖的中心坐标: - (CGPoint) centerCoordinate: (NSSet*)touches withEvent:(UIEvent*) event { UITouch* touch = [touches anyObject]; location = [touch locationInView:self]; centerCoord = [touc

我有8块瓷砖,以3x3的网格排列,有一个空白点。然而,唯一可以移动的瓦片是在空白点旁边的瓦片。

我有一个中心坐标方法,可以获取您触摸的任何瓷砖的中心坐标:

- (CGPoint) centerCoordinate: (NSSet*)touches withEvent:(UIEvent*) event {
UITouch* touch = [touches anyObject];
location = [touch locationInView:self];
centerCoord = [touch locationInView:self.superview];
centerCoord.x += self.frame.size.width/2-location.x;
centerCoord.y += self.frame.size.height/2-location.y;
self.center = centerCoord;

return centerCoord;
}
在我的touchesMoved方法中,我认为添加一个简单的if检查可以确保沿着某个x轴的任何瓷砖都可以移动:

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if ([self centerCoordinate:touches withEvent:event].x == 374) {
NSLog(@"got here");
UITouch* touch = [touches anyObject];
CGPoint tileLocation = [touch locationInView:self.superview];
tileLocation.x += self.frame.size.width/2-location.x;
tileLocation.y += self.frame.size.height/2-location.y;
self.center = tileLocation;
NSLog(@"got here2");
}
}
当我尝试移动磁贴时,“got here”和“got here 2”都会打印出来,但磁贴不会移动。如果我删除If语句,瓷砖可以自由移动

编辑:当我将触摸移动到此位置时,我可以移动不在x=374中的瓷砖。如果我在tile处移动到x=374,它会在我放开它后锁定到位

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {

NSLog(@"---1---");
UITouch* touch = [touches anyObject];
CGPoint tileLocation = [touch locationInView:self.superview];
if ([self centerCoordinate:touches withEvent:event].x == 374) {
NSLog(@"---2---");
tileLocation.x += self.frame.size.width/2-location.x;
tileLocation.y += self.frame.size.height/2-location.y;
NSLog(@"---3---");
}
self.center = tileLocation;
NSLog(@"---4---");
}

你知道是什么原因造成的吗?

如果要使瓷砖x坐标为374,你如何将它们移出374?另外,当您能够移动它们时,NSLog应该在您移动它们时更新,我假设在第一种情况下,您只在控制台中看到一次gothere/gothere 2?是的,这可能是我遇到的问题。我想我可能已经弄明白了,谢谢。