我有8个瓷砖布局在3x3网格中,有一个空白点。但是,唯一可以移动的图块就是空白点旁边的图块。
我有一个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语句,则tile可以自由移动。
编辑:当我改变触摸移动到此时,我可以移动不在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---");
}
关于导致这种情况的任何想法?