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.imagein your secondifcondition needs toimage2.imageif you need to over lapimage1andimage2center. But if you need to move the images you have to do the follow -Ex: If
image1center is at (x1, y1) andimage2center is at (x2, y2). Now the touch point (x3, y3) belongs both to imageimage1andimage2. If the new dragged is at (x4,y4), the drag amount isx4-x3andy4-y3alongx,ydirections 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.