You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.3 KiB

  1. //
  2. // FLEXScopeCarousel.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/17/19.
  6. // Copyright © 2019 Flipboard. 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) NSLayoutConstraint *heightConstraint;
  18. @property (nonatomic, readonly) id dynamicTypeObserver;
  19. @property (nonatomic, readonly) NSMutableArray *dynamicTypeHandlers;
  20. @property (nonatomic) BOOL constraintsInstalled;
  21. @end
  22. @implementation FLEXScopeCarousel
  23. - (id)initWithFrame:(CGRect)frame {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. self.backgroundColor = [FLEXColor primaryBackgroundColor];
  27. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  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. [self.collectionView setNeedsLayout];
  66. [self setNeedsUpdateConstraints];
  67. // Notify observers
  68. __typeof(self) self = weakSelf;
  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.translatesAutoresizingMaskIntoConstraints = NO;
  98. self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
  99. [self.centerXAnchor constraintEqualToAnchor:self.superview.centerXAnchor].active = YES;
  100. [self.widthAnchor constraintEqualToAnchor:self.superview.widthAnchor].active = YES;
  101. [self.topAnchor constraintEqualToAnchor:self.superview.topAnchor].active = YES;
  102. [self.collectionView pinEdgesToSuperview];
  103. _heightConstraint = [self.heightAnchor constraintEqualToConstant:100];
  104. self.heightConstraint.active = YES;
  105. self.constraintsInstalled = YES;
  106. }
  107. self.heightConstraint.constant = [self.sizingCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  108. [super updateConstraints];
  109. }
  110. #pragma mark - Public
  111. - (void)setItems:(NSArray<NSString *> *)items {
  112. NSParameterAssert(items.count);
  113. _items = items.copy;
  114. // Refresh list, select first item initially
  115. [self.collectionView reloadData];
  116. self.selectedIndex = 0;
  117. }
  118. - (void)setSelectedIndex:(NSInteger)idx {
  119. NSParameterAssert(idx < self.items.count);
  120. _selectedIndex = idx;
  121. NSIndexPath *path = [NSIndexPath indexPathForItem:idx inSection:0];
  122. [self.collectionView selectItemAtIndexPath:path
  123. animated:YES
  124. scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
  125. [self collectionView:self.collectionView didSelectItemAtIndexPath:path];
  126. }
  127. - (void)registerBlockForDynamicTypeChanges:(void (^)(FLEXScopeCarousel *))handler {
  128. [self.dynamicTypeHandlers addObject:handler];
  129. }
  130. #pragma mark - UICollectionView
  131. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  132. // if (@available(iOS 10.0, *)) {
  133. // return UICollectionViewFlowLayoutAutomaticSize;
  134. // }
  135. self.sizingCell.title = self.items[indexPath.item];
  136. return [self.sizingCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
  137. }
  138. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  139. return self.items.count;
  140. }
  141. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
  142. cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  143. FLEXCarouselCell *cell = (id)[collectionView dequeueReusableCellWithReuseIdentifier:kCarouselCellReuseIdentifier
  144. forIndexPath:indexPath];
  145. cell.title = self.items[indexPath.row];
  146. return cell;
  147. }
  148. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  149. _selectedIndex = indexPath.item; // In case self.selectedIndex didn't trigger this call
  150. if (self.selectedIndexChangedAction) {
  151. self.selectedIndexChangedAction(indexPath.row);
  152. }
  153. // TODO: dynamically choose a scroll position. Very wide items should
  154. // get "Left" while smaller items should not scroll at all, unless
  155. // they are only partially on the screen, in which case they
  156. // should get "HorizontallyCentered" to bring them onto the screen.
  157. // For now, everything goes to the left, as this has a similar effect.
  158. [collectionView scrollToItemAtIndexPath:indexPath
  159. atScrollPosition:UICollectionViewScrollPositionLeft
  160. animated:YES];
  161. [self sendActionsForControlEvents:UIControlEventValueChanged];
  162. }
  163. @end