FLEXScopeCarousel.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // FLEXScopeCarousel.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/17/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXScopeCarousel.h"
  9. #import "FLEXCarouselCell.h"
  10. #import "FLEXColor.h"
  11. #import "UIView+FLEX_Layout.h"
  12. const CGFloat kCarouselItemSpacing = 0;
  13. NSString * const kCarouselCellReuseIdentifier = @"kCarouselCellReuseIdentifier";
  14. @interface FLEXScopeCarousel () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  15. @property (nonatomic, readonly) UICollectionView *collectionView;
  16. @property (nonatomic, readonly) FLEXCarouselCell *sizingCell;
  17. @property (nonatomic, readonly) id dynamicTypeObserver;
  18. @property (nonatomic, readonly) NSMutableArray *dynamicTypeHandlers;
  19. @property (nonatomic) BOOL constraintsInstalled;
  20. @end
  21. @implementation FLEXScopeCarousel
  22. - (id)initWithFrame:(CGRect)frame {
  23. self = [super initWithFrame:frame];
  24. if (self) {
  25. self.backgroundColor = FLEXColor.primaryBackgroundColor;
  26. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  27. self.translatesAutoresizingMaskIntoConstraints = YES;
  28. _dynamicTypeHandlers = [NSMutableArray new];
  29. CGSize itemSize = CGSizeZero;
  30. if (@available(iOS 10.0, *)) {
  31. itemSize = UICollectionViewFlowLayoutAutomaticSize;
  32. }
  33. // Collection view layout
  34. UICollectionViewFlowLayout *layout = ({
  35. UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
  36. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  37. layout.sectionInset = UIEdgeInsetsZero;
  38. layout.minimumLineSpacing = kCarouselItemSpacing;
  39. layout.itemSize = itemSize;
  40. layout.estimatedItemSize = itemSize;
  41. layout;
  42. });
  43. // Collection view
  44. _collectionView = ({
  45. UICollectionView *cv = [[UICollectionView alloc]
  46. initWithFrame:CGRectZero
  47. collectionViewLayout:layout
  48. ];
  49. cv.showsHorizontalScrollIndicator = NO;
  50. cv.backgroundColor = UIColor.clearColor;
  51. cv.delegate = self;
  52. cv.dataSource = self;
  53. [cv registerClass:[FLEXCarouselCell class] forCellWithReuseIdentifier:kCarouselCellReuseIdentifier];
  54. [self addSubview:cv];
  55. cv;
  56. });
  57. // Sizing cell
  58. _sizingCell = [FLEXCarouselCell new];
  59. self.sizingCell.title = @"NSObject";
  60. // Dynamic type
  61. __weak __typeof(self) weakSelf = self;
  62. _dynamicTypeObserver = [NSNotificationCenter.defaultCenter
  63. addObserverForName:UIContentSizeCategoryDidChangeNotification
  64. object:nil queue:nil usingBlock:^(NSNotification *note) {
  65. __typeof(self) self = weakSelf;
  66. [self.collectionView setNeedsLayout];
  67. [self setNeedsUpdateConstraints];
  68. // Notify observers
  69. for (void (^block)(FLEXScopeCarousel *) in self.dynamicTypeHandlers) {
  70. block(self);
  71. }
  72. }
  73. ];
  74. }
  75. return self;
  76. }
  77. - (void)dealloc {
  78. [NSNotificationCenter.defaultCenter removeObserver:self.dynamicTypeObserver];
  79. }
  80. #pragma mark - Overrides
  81. - (void)drawRect:(CGRect)rect {
  82. [super drawRect:rect];
  83. CGFloat width = 1.f / UIScreen.mainScreen.scale;
  84. // Draw hairline
  85. CGContextRef context = UIGraphicsGetCurrentContext();
  86. CGContextSetStrokeColorWithColor(context, FLEXColor.hairlineColor.CGColor);
  87. CGContextSetLineWidth(context, width);
  88. CGContextMoveToPoint(context, 0, rect.size.height - width);
  89. CGContextAddLineToPoint(context, rect.size.width, rect.size.height - width);
  90. CGContextStrokePath(context);
  91. }
  92. + (BOOL)requiresConstraintBasedLayout {
  93. return YES;
  94. }
  95. - (void)updateConstraints {
  96. if (!self.constraintsInstalled) {
  97. self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  98. [self.collectionView flex_pinEdgesToSuperview];
  99. self.constraintsInstalled = YES;
  100. }
  101. [super updateConstraints];
  102. }
  103. - (CGSize)intrinsicContentSize {
  104. return CGSizeMake(
  105. UIViewNoIntrinsicMetric,
  106. [self.sizingCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height
  107. );
  108. }
  109. #pragma mark - Public
  110. - (void)setItems:(NSArray<NSString *> *)items {
  111. NSParameterAssert(items.count);
  112. _items = items.copy;
  113. // Refresh list, select first item initially
  114. [self.collectionView reloadData];
  115. self.selectedIndex = 0;
  116. }
  117. - (void)setSelectedIndex:(NSInteger)idx {
  118. NSParameterAssert(idx < self.items.count);
  119. _selectedIndex = idx;
  120. NSIndexPath *path = [NSIndexPath indexPathForItem:idx inSection:0];
  121. [self.collectionView selectItemAtIndexPath:path
  122. animated:YES
  123. scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
  124. [self collectionView:self.collectionView didSelectItemAtIndexPath:path];
  125. }
  126. - (void)registerBlockForDynamicTypeChanges:(void (^)(FLEXScopeCarousel *))handler {
  127. [self.dynamicTypeHandlers addObject:handler];
  128. }
  129. #pragma mark - UICollectionView
  130. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  131. // if (@available(iOS 10.0, *)) {
  132. // return UICollectionViewFlowLayoutAutomaticSize;
  133. // }
  134. self.sizingCell.title = self.items[indexPath.item];
  135. return [self.sizingCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
  136. }
  137. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  138. return self.items.count;
  139. }
  140. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
  141. cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  142. FLEXCarouselCell *cell = (id)[collectionView dequeueReusableCellWithReuseIdentifier:kCarouselCellReuseIdentifier
  143. forIndexPath:indexPath];
  144. cell.title = self.items[indexPath.row];
  145. return cell;
  146. }
  147. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  148. _selectedIndex = indexPath.item; // In case self.selectedIndex didn't trigger this call
  149. if (self.selectedIndexChangedAction) {
  150. self.selectedIndexChangedAction(indexPath.row);
  151. }
  152. // TODO: dynamically choose a scroll position. Very wide items should
  153. // get "Left" while smaller items should not scroll at all, unless
  154. // they are only partially on the screen, in which case they
  155. // should get "HorizontallyCentered" to bring them onto the screen.
  156. // For now, everything goes to the left, as this has a similar effect.
  157. [collectionView scrollToItemAtIndexPath:indexPath
  158. atScrollPosition:UICollectionViewScrollPositionLeft
  159. animated:YES];
  160. [self sendActionsForControlEvents:UIControlEventValueChanged];
  161. }
  162. @end