1
votes

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!

2

2 Answers

0
votes

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

0
votes

In the end, i chose making custom view as i feel that it would be less messy in the UIView subclass since there are already many codes in there... it is a personal choice for me, but i really want to find out if i should have done otherwise, or any more elegant alternative... maybe from a memory aspect or from overlay aspect