Iphone – Dragging 1 of 52 cards in UIView. Which is a better method

iphonexcode

Currently I have 52 cards UIImageViews in IB UIView.

And my objective is to drag 1 of this 52 cards into a Square, and if the the card is dropped into the Square, it will stay there. Otherwise, it will snap back it's original position.

MY question is which is a better method?

  1. Creating a custom class NSObject and changing every card in IB into this class and using this code in the .m file.
    `- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.superview];

    self.center = location;
    }

  2. Declaring 52 IBOutlets and make them respond to the same action

    i)Subsidiary question: Is there a way i do not need to declare all 52 IBOutlets?
    `
    Thank you in advance!

Best Solution

It will be best to add the cards through a for loop programmatically setting the tag of each card like x+1 to x+52, make them respond to same action and in check the frame for the matching frame of your card in -(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { UITouch *touch = [touches anyObject]; this is a little bit tricky but much less effort is required and the things will be much more handlable.

Hope this will give you a good idea

Related Question