0

我在 3x3 网格中布置了 8 个瓷砖,其中有一个空白点。但是,唯一可以移动的瓷砖是空白点旁边的瓷砖。

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

- (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");
}
}

当我尝试移动瓷砖时,“到了这里”和“到了这里2”都打印出来了,但瓷砖没有移动。如果我删除 if 语句,瓷砖可以自由移动。

编辑:当我改变触摸移动到这个时,我可以移动不在 x = 374 中的图块。如果我在图块处移动到 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---");
}

有什么想法会导致这种情况吗?

4

0 回答 0