Kamiel

custom layout collection view

Mar 26th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  FFMarketSelector.m
  3. //  FMF Football
  4. //
  5. //  Created by Samuel Sutch on 9/24/13.
  6. //  Copyright (c) 2013 FairMarketFantasy. All rights reserved.
  7. //
  8.  
  9. #import "FFMarketSelector.h"
  10.  
  11. @interface FFMarketSelector () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  12.  
  13. @property(nonatomic) UICollectionView* collectionView;
  14. @property(nonatomic) UICollectionViewFlowLayout* flowLayout;
  15.  
  16. @end
  17.  
  18. @implementation FFMarketSelector
  19.  
  20. - (id)initWithFrame:(CGRect)frame
  21. {
  22.     self = [super initWithFrame:frame];
  23.     if (self) {
  24.         _flowLayout = [[UICollectionViewFlowLayout alloc] init];
  25.         _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  26.         _flowLayout.sectionInset = UIEdgeInsetsZero;
  27.         _flowLayout.minimumInteritemSpacing = 0;
  28.         _flowLayout.minimumLineSpacing = 0;
  29.  
  30.         _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(45, 0, frame.size.width - 90, frame.size.height)
  31.                                              collectionViewLayout:_flowLayout];
  32.         _collectionView.delegate = self;
  33.         _collectionView.dataSource = self;
  34.         _collectionView.pagingEnabled = YES;
  35.         _collectionView.backgroundColor = [FFStyle white];
  36.         _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  37.         _collectionView.alwaysBounceHorizontal = YES;
  38.         _collectionView.showsHorizontalScrollIndicator = NO;
  39.         [_collectionView registerClass:[UICollectionViewCell class]
  40.             forCellWithReuseIdentifier:@"MarketCell"];
  41.         [self addSubview:_collectionView];
  42.  
  43.         UIButton* left = [UIButton buttonWithType:UIButtonTypeCustom];
  44.         [left setImage:[UIImage imageNamed:@"leftshuttle.png"]
  45.               forState:UIControlStateNormal];
  46.         left.frame = CGRectMake(10, 0, 35, frame.size.height);
  47.         left.autoresizingMask = UIViewAutoresizingNone;
  48.         [left addTarget:self
  49.                       action:@selector(left:)
  50.             forControlEvents:UIControlEventTouchUpInside];
  51.         [self addSubview:left];
  52.  
  53.         UIButton* right = [UIButton buttonWithType:UIButtonTypeCustom];
  54.         [right setImage:[UIImage imageNamed:@"rightshuttle.png"]
  55.                forState:UIControlStateNormal];
  56.         right.frame = CGRectMake(frame.size.width - 35 - 10, 0, 35, frame.size.height);
  57.         right.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  58.         [right addTarget:self
  59.                       action:@selector(right:)
  60.             forControlEvents:UIControlEventTouchUpInside];
  61.         [self addSubview:right];
  62.     }
  63.     return self;
  64. }
  65.  
  66. - (void)left:(UIButton*)sender
  67. {
  68.     NSArray* selected = [self.collectionView indexPathsForVisibleItems];
  69.     if (selected && selected.count) {
  70.         NSIndexPath* path = [selected objectAtIndex:0];
  71.         if (path.item == 0)
  72.             return; // already at the first item
  73.         [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:path.item - 1
  74.                                                                          inSection:0]
  75.                                     atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
  76.                                             animated:YES];
  77.         _selectedMarket = self.markets[path.item - 1];
  78.         if (self.delegate && [self.delegate respondsToSelector:@selector(didUpdateToNewMarket:)]) {
  79.             [self.delegate didUpdateToNewMarket:_selectedMarket];
  80.         }
  81.     }
  82. }
  83.  
  84. - (void)right:(UIButton*)sender
  85. {
  86.     NSArray* selected = [self.collectionView indexPathsForVisibleItems];
  87.     if (selected && selected.count) {
  88.         NSIndexPath* path = [selected lastObject];
  89.         if (path.item == (self.markets.count - 1))
  90.             return; // already at last item
  91.         [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:path.item + 1
  92.                                                                          inSection:0]
  93.                                     atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
  94.                                             animated:YES];
  95.         _selectedMarket = self.markets[path.item + 1];
  96.         if (self.delegate && [self.delegate respondsToSelector:@selector(didUpdateToNewMarket:)]) {
  97.             [self.delegate didUpdateToNewMarket:_selectedMarket];
  98.         }
  99.     }
  100. }
  101.  
  102. - (void)setMarkets:(NSArray*)markets
  103. {
  104.     if (self.markets == markets) {
  105.         return;
  106.     }
  107.     _markets = [markets copy];
  108.     [self.collectionView reloadData];
  109.     if (self.markets.count > 0) {
  110.         self.selectedMarket = self.markets.firstObject;
  111.      }
  112.     self.selectedMarket = nil;
  113. }
  114.  
  115. - (void)setSelectedMarket:(NSUInteger)selectedMarket
  116. {
  117.     if (self.selectedMarket == selectedMarket) {
  118.         return;
  119.     }
  120.     if (self.delegate &&
  121.         self.delegate.markets.count <= selectedMarket
  122.         ) {
  123.         return;
  124.     }
  125.     _selectedMarket = selectedMarket;
  126.     [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedMarket
  127.                                                                      inSection:0]
  128.                                 atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
  129.                                         animated:YES];
  130. }
  131.  
  132. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
  133. {
  134.     return 1;
  135. }
  136.  
  137. - (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
  138. {
  139.     return [self.markets count];
  140. }
  141.  
  142. - (CGSize)collectionView:(UICollectionView*)collectionView
  143.                     layout:(UICollectionViewLayout*)collectionViewLayout
  144.     sizeForItemAtIndexPath:(NSIndexPath*)indexPath
  145. {
  146.     return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
  147. }
  148.  
  149. - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
  150. {
  151.     UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MarketCell"
  152.                                                                            forIndexPath:indexPath];
  153.     [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  154.  
  155.     FFMarket* market = self.markets[indexPath.item];
  156.  
  157.     UILabel* marketLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 30)];
  158.     marketLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  159.     marketLabel.font = [FFStyle boldFont:17];
  160.     marketLabel.textColor = [FFStyle darkGreyTextColor];
  161.     marketLabel.backgroundColor = [UIColor clearColor];
  162.     marketLabel.textAlignment = NSTextAlignmentCenter;
  163.  
  164.     if (market.name && market.name.length) {
  165.         marketLabel.text = market.name;
  166.     } else {
  167.         marketLabel.text = NSLocalizedString(@"Market", nil);
  168.     }
  169.  
  170.     NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
  171.     [dateFormatter setDateFormat:@"E d @ h:mm a"];
  172.  
  173.     UILabel* timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, cell.contentView.frame.size.width, 25)];
  174.     timeLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
  175.     timeLabel.font = [FFStyle regularFont:17.f];
  176.     timeLabel.textColor = [FFStyle black];
  177.     timeLabel.textAlignment = NSTextAlignmentCenter;
  178.     timeLabel.backgroundColor = [UIColor clearColor];
  179.     timeLabel.text = [dateFormatter stringFromDate:market.startedAt];
  180.  
  181.     [cell.contentView addSubview:timeLabel];
  182.     [cell.contentView addSubview:marketLabel];
  183.  
  184.     return cell;
  185. }
  186.  
  187. - (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
  188. {
  189.     NSArray* visible = [self.collectionView indexPathsForVisibleItems];
  190.     if (visible && visible.count) {
  191.         NSIndexPath* path = visible[0];
  192.         if (self.delegate && [self.delegate respondsToSelector:@selector(didUpdateToNewMarket:)]) {
  193.             [self.delegate didUpdateToNewMarket:self.markets[path.item]];
  194.         }
  195.     }
  196. }
  197.  
  198. @end
Advertisement
Add Comment
Please, Sign In to add comment