我正在使用 Xcode 开发一个项目,我使用一堆包含 UIImage 的小 UIImageViews 设置了一个棋盘,并将它们放置在一个更大的 UIImageView 中,代表棋盘。
到目前为止,我设法正确使用 UITouches 在棋盘上自由拖放 UIImageViews(碎片)。但是,当我放下它们时,它们就会降落在我释放它们的任何地方,这自然是应该的,但这不是我想要的效果。
理想情况下,UIImageViews 不应该移动到任何地方,我只想将 UIImage 从当前选择的 UIImageView 转移到下一个 UIImageView。我相信不以这种方式进行拖放会简单得多,而是我使用点击(或点击)。我点击一个 UIImageView,然后点击下一个 UIImageView 将从前一个 UIImageView 中获取 UIImage。想一想如何在不拖放图像的情况下下棋。
那么有人对如何在这里进行有任何建议吗?提前致谢!
编辑:这里有一些代码来显示我现在在做什么:
//================================================================================
// I have two arrays containing UIImageViews each, one representing board cells,
// the other one the pieces. The below functions handle the touch and drag actions.
// What I need to do is change this somehow so that the UIImageViews representing
// the pieces do one of the following things:
// 1. When the pieces are dragged, you can drop them and they center themselves
// on top of the UIImage views that represent the cells of the board.
// 2. I have an array of UIImageViews containing the cells, when I click on a
// UIImageView that has the image of a piece, it is copied and pasted onto
// the next UIImageView that I click, of course controls will be set to determine
// a valid move I can handle doing this part.
//================================================================================
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//get location of the current touch event
UITouch *touch = [[event allTouches] anyObject];
//get touch location for the touched image (self used instead of touch)
CGPoint touchLocation = [touch locationInView:[self view]];
//select and give current location to the selected view
for (UIImageView *piece in chessCells) {
if([touch view] == piece){
piece.center = touchLocation;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//allow the selected event (in our case a UIImageView) to be dragged
[self touchesBegan:touches withEvent:event];
}
最初我把这个问题留了下来,因为我知道我必须以某种方式改变我的实现,但我希望这段代码有助于理解我所面临的问题:Drag and Drop anywhere without任何限制,我只是不知道如何控制这从发生。