tags:

views:

44

answers:

2

Hi people! 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!

A: 

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

Madhup
Thank you so much!!!! but how can you add IBOutlet using a for loop?
zerlphr
A: 

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

zerlphr
you need to add subviews for your cards like [self.view addSubview:card]; where card is of same ui type as you want in IBOutlet, it will give you same functionality
Madhup
the problem is that i need to arrange the cards in a regular order.. so i can't simply addSubView.the cards are arranged into 4 rows of each suit....
zerlphr