質問

I'm newbie of iOS.

I create a card game and set the card in UIView class.

This is my storybook view.
enter image description here

I want to use the swipe gesture in every card.

I hope when I swipe one of the card, then the card will flip and show the suit and rank.

May I set the gesture except the way that drag and drop the swipe library to every card?

Besides, the other question is how can I know which card the user is swiping.

I declare the UIView in an array.

@property (strong, nonatomic) IBOutletCollection(PlayingCardView) NSArray *playingCardView;

Does anyone can help me?

Really thank you.

役に立ちましたか?

解決

Based on the screenshot, that you have to do is not to set the cards in an UIView class but put in a UICollectionView class.

Basically, you have to set Delegate and DataSource protocol methods.

Here is a quick sample of how you should do this view:

  1. Create an UICollectionView
  2. Implement Delegate and DataSource protocol methods.
  3. Create a subclass of UICollectionViewCell and use it for each cell of your UICollectionView. In your case, the cells represent the cards.

By implementing this delegate method:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

You will handle tap gesture.

To handle different events on your card you can add gesture on each card by adding it into the subclass of UICollectionViewCell that you created before.

There are a lot of good articles about how implement an UICollectionView to help you on internet.

Moreover, not using UICollectionView to display a grid of contents is harmful for your code and the performance of your app:

  • UICollectionView provides useful delegate methods which permits to reduce the needed code to create a grid.
  • UICollectionView is more optimized than display dozen of subviews to make a grid in an UIView.

I hope my answer helps you.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top