Objective-c – Draggable cards (touch enumeration) issue

cocoa-touchiphoneobjective c

I'm trying to let a player tap, drag and release a card from a fanned stack on the screen to a 4×4 field on the board. My cards are instantiated from a custom class that inherits from the UIImageView class.

I started with the Touches sample app, and I modified the event handlers for touches to iterate over my player's card hand instead of the 3 squares the sample app allows you to move on screen. Everything works, until that is, I move the card I'm dragging near another card. I'm really drawing a blank here for the logic to get the cards to behave properly. Here's my code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
NSUInteger numTaps = [[touches anyObject] tapCount];
if(numTaps = 1) {
    for (UITouch *touch in touches) {
        [self dispatchFirstTouchAtPoint:[touch locationInView: self.boardCardView] forEvent:nil];
        }   
    }
}

-(void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event
{
    for (int i = 0; i<5; i++)
    {
        UIImageView *touchedCard = boardBuffer[i];
        if (CGRectContainsPoint([touchedCard frame], touchPoint)) {
            [self animateFirstTouchAtPoint:touchPoint forView:touchedCard];
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    NSUInteger touchCount = 0;
    for (UITouch *touch in touches){
        [self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self.boardCardView]];
        touchCount++;
    }       
}

My questions are:

  1. How do I get the touch logic to disallow other cards from being picked up by a dragging finger?

  2. Is there anyway I can only enumerate the objects that are directly below a player's finger and explicitly disable other objects from responding?

Thanks!

Best Answer

From your description, "How do I get the touch logic to disallow other cards from being picked up by a dragging finger?" it seems as though your finger is like a magnet picking up cards as you drag it along?!? I haven't run into that, specifically, but by putting the event handling into your card class you can have each card decide whether it should respond to the touch or to pass it up its responder chain to another one that does.

I think you should review the section of the iPhone Application Programming Guide on Event Handling. Specifically the chapter on Responder Objects and the Responder Chain.

Related Topic