Reputation: 3792
I am working on a project using Xcode, I've set up a checker board using a bunch of little UIImageViews containing UIImages of the pieces and laid them out inside a larger UIImageView, representing the checker board.
So far I managed to properly use UITouches to drag and drop the UIImageViews (the pieces) freely across the checker board. However when I drop them, they just land wherever I release them, naturally as it should but this is not the desired effect I am looking for.
Ideally the UIImageViews should not move anywhere, I only want to transfer the UIImage from the currently selected UIImageView to the next UIImageView. I believe that it would be a lot simpler not to do drag and drop in this fashion but that instead I use clicks (or taps). I tap on an UIImageView, then tap on the next and that UIImageView will get the UIImage from the previous UIImageView. Think about how you would play chess without drag/drop the images.
So does anyone have any suggestions as to how to proceed here? Thanks in advance!
Edit: here is some code to show what I am doing now:
//================================================================================
// 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];
}
Initially I left this question open ended because I know I have to change my implementation somehow, but I hope that this code helps understand the issue that I am facing: Drag and Drop anywhere without any restrictions, I just don't know how to control this from happening.
Upvotes: 1
Views: 535
Reputation: 125037
A UIImage is just data -- it doesn't draw itself anywhere. You need a UIImageView (or some other view or layer class) to draw the image on the screen. So, if you simply move the image from one image view to another, the image will disappear at one location and appear at the other -- there won't be any animation as the piece moves from one square to another.
I'd suggest using Core Animation to move the image views themselves. That'll provide some feedback so that the user has a better understanding of what piece moved where. That's particularly important in a game like chess, where part of the challenge is to notice which pieces are able to move where.
The key to getting the pieces positioned properly on the square is simply to move them to the desired position. For example, you might move a piece by changing the center
of the corresponding image view from the center of one square to the center of another.
Upvotes: 1
Reputation: 1535
How about when the tap occurs on a UIImageView make a copy of it, and use that copy to drag around. When the release is done use the UIImage in that copied UIImageView to change the UIImageView being dropped on, then delete the copied UIImageView?
Upvotes: 1