I am trying to have multiple objects in a touchesbegan method (2 UIImageViews)
I am using the below code but isn't working. No errors but the location is just messed up. What should I do instead?
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
if (image1.image == [UIImage imageNamed:@"ball.png"]){
CGPoint location = [touch locationInView:touch.view];
image1.center = location;
}
if (image1.image == [UIImage imageNamed:@"ball2.png"]){
CGPoint location = [touch locationInView:touch.view];
image2.center = location;
}
}
I guess,
image1.image
in your secondif
condition needs toimage2.image
if you need to over lapimage1
andimage2
center. But if you need to move the images you have to do the follow -Ex: If
image1
center is at (x1, y1) andimage2
center is at (x2, y2). Now the touch point (x3, y3) belongs both to imageimage1
andimage2
. If the new dragged is at (x4,y4), the drag amount isx4-x3
andy4-y3
alongx,y
directions respectively. Add this drag amount to both the image's center for the image to appear at the new location.Pseudo Code
Download the source code of iPhone Game Dev chapter 3 and see how images are being moved.