3x3 グリッドに 8 つのタイルを配置し、1 つの空白スポットを配置しました。ただし、移動できるタイルは空白の隣にあるタイルのみです。
タッチしたタイルの中心座標を取得する 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");
}
}
タイルを移動しようとすると、"got here" と "got here2" の両方が出力されますが、タイルは移動しません。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---");
}
これを引き起こしている原因についてのアイデアはありますか?