Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi,

I am stuck while trying to create multiple draggable UIImageViews in Xcode 4. I am also trying to get them to have an action when the collide with another UIImageView.

Thanks!
 
Dragging Images

Creating draggable images and collision testing with them shouldn't be too hard, but it will require quite a bit of code (enough so that I'm not going to post it all). Read up on UIGestureRecognizers. In particular, pay attention to the UIPanGestureRecognizer, it's what's used to detect dragging on the screen.
Edit: As for collision testing, throw all of your imageViews into an array and then loop through them all to see if any of them touch.
 
Edit: As for collision testing, throw all of your imageViews into an array and then loop through them all to see if any of them touch.

Using CGRectIntersectsRect(image1.frame, image2.frame)

where image1 and image 2 are the names of your images will be very useful to you in collision detections.
 
Hi,

I have had some luck when using the code below:
Code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	add1.center = location;
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	[self touchesBegan:touches withEvent:event];
}

But now, I do not know how to apply this code to multiple images. . .
 
Hi,

I have had some luck when using the code below:
Code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	add1.center = location;
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	[self touchesBegan:touches withEvent:event];
}

But now, I do not know how to apply this code to multiple images. . .

Rather than simply accept the touch, have it check which image the touch is inside of. Something like CGRectContainsPoint(img1.frame, location) will check if the touch location is within the frame of image 1.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.