|
10 | 10 | #import "PlayingCard.h" |
11 | 11 | #import "PlayingCardCell.h" |
12 | 12 |
|
13 | | -@interface LXCollectionViewController () |
| 13 | +// LX_LIMITED_MOVEMENT: |
| 14 | +// 0 = Any card can move anywhere |
| 15 | +// 1 = Only Spade/Club can move within same rank |
14 | 16 |
|
15 | | -@end |
| 17 | +#define LX_LIMITED_MOVEMENT 0 |
16 | 18 |
|
17 | 19 | @implementation LXCollectionViewController |
18 | 20 |
|
19 | | -- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
20 | | -{ |
21 | | - self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
22 | | - if (self) { |
23 | | - // Custom initialization |
24 | | - } |
25 | | - return self; |
26 | | -} |
27 | | - |
28 | | -- (void)awakeFromNib { |
29 | | - [super awakeFromNib]; |
| 21 | +- (void)viewDidLoad { |
| 22 | + [super viewDidLoad]; |
30 | 23 |
|
31 | 24 | self.deck = [self constructsDeck]; |
32 | 25 | } |
33 | 26 |
|
34 | 27 | - (NSMutableArray *)constructsDeck { |
35 | | - NSMutableArray *theDeck = [NSMutableArray arrayWithCapacity:52]; |
36 | | - for (NSInteger theRank = 1; theRank <= 13; theRank++) { |
| 28 | + NSMutableArray *newDeck = [NSMutableArray arrayWithCapacity:52]; |
| 29 | + |
| 30 | + for (NSInteger rank = 1; rank <= 13; rank++) { |
37 | 31 | // Spade |
38 | 32 | { |
39 | | - PlayingCard *thePlayingCard = [[PlayingCard alloc] init]; |
40 | | - thePlayingCard.suit = PlayingCardSuitSpade; |
41 | | - thePlayingCard.rank = theRank; |
42 | | - [theDeck addObject:thePlayingCard]; |
| 33 | + PlayingCard *playingCard = [[PlayingCard alloc] init]; |
| 34 | + playingCard.suit = PlayingCardSuitSpade; |
| 35 | + playingCard.rank = rank; |
| 36 | + [newDeck addObject:playingCard]; |
43 | 37 | } |
44 | 38 |
|
45 | 39 | // Heart |
46 | 40 | { |
47 | | - PlayingCard *thePlayingCard = [[PlayingCard alloc] init]; |
48 | | - thePlayingCard.suit = PlayingCardSuitHeart; |
49 | | - thePlayingCard.rank = theRank; |
50 | | - [theDeck addObject:thePlayingCard]; |
| 41 | + PlayingCard *playingCard = [[PlayingCard alloc] init]; |
| 42 | + playingCard.suit = PlayingCardSuitHeart; |
| 43 | + playingCard.rank = rank; |
| 44 | + [newDeck addObject:playingCard]; |
51 | 45 | } |
52 | 46 |
|
53 | 47 | // Club |
54 | 48 | { |
55 | | - PlayingCard *thePlayingCard = [[PlayingCard alloc] init]; |
56 | | - thePlayingCard.suit = PlayingCardSuitClub; |
57 | | - thePlayingCard.rank = theRank; |
58 | | - [theDeck addObject:thePlayingCard]; |
| 49 | + PlayingCard *playingCard = [[PlayingCard alloc] init]; |
| 50 | + playingCard.suit = PlayingCardSuitClub; |
| 51 | + playingCard.rank = rank; |
| 52 | + [newDeck addObject:playingCard]; |
59 | 53 | } |
60 | 54 |
|
61 | 55 | // Diamond |
62 | 56 | { |
63 | | - PlayingCard *thePlayingCard = [[PlayingCard alloc] init]; |
64 | | - thePlayingCard.suit = PlayingCardSuitDiamond; |
65 | | - thePlayingCard.rank = theRank; |
66 | | - [theDeck addObject:thePlayingCard]; |
| 57 | + PlayingCard *playingCard = [[PlayingCard alloc] init]; |
| 58 | + playingCard.suit = PlayingCardSuitDiamond; |
| 59 | + playingCard.rank = rank; |
| 60 | + [newDeck addObject:playingCard]; |
67 | 61 | } |
68 | 62 | } |
69 | | - return theDeck; |
| 63 | + |
| 64 | + return newDeck; |
70 | 65 | } |
71 | 66 |
|
72 | | -- (void)viewDidLoad { |
73 | | - [super viewDidLoad]; |
74 | | - // Do any additional setup after loading the view. |
| 67 | +#pragma mark - UICollectionViewDataSource methods |
| 68 | + |
| 69 | +- (NSInteger)collectionView:(UICollectionView *)theCollectionView numberOfItemsInSection:(NSInteger)theSectionIndex { |
| 70 | + return self.deck.count; |
75 | 71 | } |
76 | 72 |
|
77 | | -- (void)didReceiveMemoryWarning { |
78 | | - [super didReceiveMemoryWarning]; |
79 | | - // Dispose of any resources that can be recreated. |
| 73 | +- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { |
| 74 | + PlayingCard *playingCard = [self.deck objectAtIndex:indexPath.item]; |
| 75 | + PlayingCardCell *playingCardCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayingCardCell" forIndexPath:indexPath]; |
| 76 | + playingCardCell.playingCard = playingCard; |
| 77 | + |
| 78 | + return playingCardCell; |
80 | 79 | } |
81 | 80 |
|
82 | | -#pragma mark - UICollectionViewDataSource methods |
| 81 | +#pragma mark - LXReorderableCollectionViewDataSource methods |
83 | 82 |
|
84 | | -- (NSInteger)collectionView:(UICollectionView *)theCollectionView numberOfItemsInSection:(NSInteger)theSectionIndex { |
85 | | - switch (theSectionIndex) { |
86 | | - case 0: { |
87 | | - return [[self valueForKeyPath:@"deck.@count"] integerValue]; |
88 | | - } break; |
89 | | - default: { |
90 | | - return 0; |
91 | | - } break; |
| 83 | +- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath { |
| 84 | + PlayingCard *playingCard = [self.deck objectAtIndex:fromIndexPath.item]; |
| 85 | + |
| 86 | + [self.deck removeObjectAtIndex:fromIndexPath.item]; |
| 87 | + [self.deck insertObject:playingCard atIndex:toIndexPath.item]; |
| 88 | +} |
| 89 | + |
| 90 | +- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath { |
| 91 | +#if LX_LIMITED_MOVEMENT == 1 |
| 92 | + PlayingCard *playingCard = [deck objectAtIndex:indexPath.item]; |
| 93 | + |
| 94 | + switch (playingCard.suit) { |
| 95 | + case PlayingCardSuitSpade: |
| 96 | + case PlayingCardSuitClub: |
| 97 | + return YES; |
| 98 | + default: |
| 99 | + return NO; |
92 | 100 | } |
| 101 | +#else |
| 102 | + return YES; |
| 103 | +#endif |
93 | 104 | } |
94 | 105 |
|
95 | | -- (UICollectionViewCell *)collectionView:(UICollectionView *)theCollectionView cellForItemAtIndexPath:(NSIndexPath *)theIndexPath { |
96 | | - NSInteger theSectionIndex = theIndexPath.section; |
97 | | - NSInteger theItemIndex = theIndexPath.item; |
98 | | - switch (theSectionIndex) { |
99 | | - case 0: { |
100 | | - PlayingCard *thePlayingCard = [self.deck objectAtIndex:theItemIndex]; |
101 | | - PlayingCardCell *thePlayingCardCell = [theCollectionView dequeueReusableCellWithReuseIdentifier:@"PlayingCardCell" forIndexPath:theIndexPath]; |
102 | | - thePlayingCardCell.playingCard = thePlayingCard; |
103 | | - return thePlayingCardCell; |
104 | | - } break; |
105 | | - default: { |
106 | | - return nil; |
107 | | - } break; |
| 106 | +- (BOOL)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath canMoveToIndexPath:(NSIndexPath *)toIndexPath { |
| 107 | +#if LX_LIMITED_MOVEMENT == 1 |
| 108 | + PlayingCard *fromPlayingCard = [deck objectAtIndex:fromIndexPath.item]; |
| 109 | + PlayingCard *toPlayingCard = [deck objectAtIndex:toIndexPath.item]; |
| 110 | + |
| 111 | + switch (toPlayingCard.suit) { |
| 112 | + case PlayingCardSuitSpade: |
| 113 | + case PlayingCardSuitClub: |
| 114 | + return fromPlayingCard.rank == toPlayingCard.rank; |
| 115 | + default: |
| 116 | + return NO; |
108 | 117 | } |
| 118 | +#else |
| 119 | + return YES; |
| 120 | +#endif |
109 | 121 | } |
110 | 122 |
|
111 | | -#pragma mark - LXReorderableCollectionViewDelegateFlowLayout methods |
| 123 | +#pragma mark - LXReorderableCollectionViewDelegate methods |
| 124 | + |
| 125 | +- (void)collectionView:(UICollectionView *)collectionView willBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath { |
| 126 | + NSLog(@"will begin drag"); |
| 127 | +} |
| 128 | + |
| 129 | +- (void)collectionView:(UICollectionView *)collectionView didBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath { |
| 130 | + NSLog(@"did begin drag"); |
| 131 | +} |
| 132 | + |
| 133 | +- (void)collectionView:(UICollectionView *)collectionView willEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath { |
| 134 | + NSLog(@"will end drag"); |
| 135 | +} |
112 | 136 |
|
113 | | -- (void)collectionView:(UICollectionView *)theCollectionView layout:(UICollectionViewLayout *)theLayout itemAtIndexPath:(NSIndexPath *)theFromIndexPath willMoveToIndexPath:(NSIndexPath *)theToIndexPath { |
114 | | - id theFromItem = [self.deck objectAtIndex:theFromIndexPath.item]; |
115 | | - [self.deck removeObjectAtIndex:theFromIndexPath.item]; |
116 | | - [self.deck insertObject:theFromItem atIndex:theToIndexPath.item]; |
| 137 | +- (void)collectionView:(UICollectionView *)collectionView didEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath { |
| 138 | + NSLog(@"did end drag"); |
117 | 139 | } |
118 | 140 |
|
119 | 141 | @end |
0 commit comments