views:

90

answers:

2

We are adding drag and drop functionality to what is to become a sports field with positions for players.

The positions are mapped out using Interface Builder with each being a separate UIImageView.

We want to be able to drag player images from bench positions from the side of the screen onto positions on the field.

How best can we detect when the selected player which is being moved around collides with an existing gamePosition imageView?

We are looking for a way to detect if there is a view or ImageView under the current location.

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [[event allTouches] anyObject];
  CGPoint location = [touch locationInView:touch.view];
  tile1.center = location;  

  if gamePositionExistsAtCurrentLocation(location) { //want something like this
    [tile1 setBackgroundColor:[UIColor blueColor]]; 
  } else {
   [tile1 setBackgroundColor:[UIColor yellowColor]]; 
  }
}
+1  A: 

check if the frame of the item you are moving intersects with the frame from on of your subviews

for (UIView *anotherView in self.subviews) {
    if (movingView == anotherView)
        continue;
    if (CGRectIntersectsRect(movingView.frame, anotherView.frame)) {
        // Do something
    }
}

If I were you, I would add all game relevant items to an NSArray and for-loop through this. So you don't detect collisions with subviews that have nothing to do with your game like labels and so on.

fluchtpunkt
So the idea is that every time I move I run through an array of objects and see if they match my current location (but using frame instead as you mention)?
Evolve
Is there any apple provided methods which I can use to let me know which subviews currently intersect with a specified point?
Evolve
+1  A: 

Also might want to consider view.center with CGRectContainsPoint()

Nimrod
The method takes the following format: CGRectContainsRect(<#CGRect rect1#>, <#CGRect rect2#>) how do I represent view.center or location.centre as a rect?
Evolve
Not CGRectContainsRect(), I was saying use CGRectContainsPoint(). Don't use the first one unless your drop rectangle is significantly bigger because that only returns true if one rectangle is entirely inside another.
Nimrod
Sorry my bad.. miss-read your post.. thanks. Sorted it out. Your suggestion has been very helpful.
Evolve