FLEXExplorerToolbar.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // FLEXExplorerToolbar.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/4/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXExplorerToolbar.h"
  9. #import "FLEXToolbarItem.h"
  10. #import "FLEXResources.h"
  11. #import "FLEXUtility.h"
  12. @interface FLEXExplorerToolbar ()
  13. @property (nonatomic, strong, readwrite) FLEXToolbarItem *selectItem;
  14. @property (nonatomic, strong, readwrite) FLEXToolbarItem *moveItem;
  15. @property (nonatomic, strong, readwrite) FLEXToolbarItem *globalsItem;
  16. @property (nonatomic, strong, readwrite) FLEXToolbarItem *closeItem;
  17. @property (nonatomic, strong, readwrite) FLEXToolbarItem *hierarchyItem;
  18. @property (nonatomic, strong, readwrite) UIView *dragHandle;
  19. @property (nonatomic, strong) UIImageView *dragHandleImageView;
  20. @property (nonatomic, strong) UIView *selectedViewDescriptionContainer;
  21. @property (nonatomic, strong) UIView *selectedViewColorIndicator;
  22. @property (nonatomic, strong) UILabel *selectedViewDescriptionLabel;
  23. @end
  24. @implementation FLEXExplorerToolbar
  25. - (id)initWithFrame:(CGRect)frame
  26. {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.dragHandle = [[UIView alloc] init];
  30. self.dragHandle.backgroundColor = [FLEXToolbarItem defaultBackgroundColor];
  31. [self addSubview:self.dragHandle];
  32. UIImage *dragHandle = [FLEXResources dragHandle];
  33. self.dragHandleImageView = [[UIImageView alloc] initWithImage:dragHandle];
  34. [self.dragHandle addSubview:self.dragHandleImageView];
  35. UIImage *globalsIcon = [FLEXResources globeIcon];
  36. self.globalsItem = [FLEXToolbarItem toolbarItemWithTitle:@"menu" image:globalsIcon];
  37. UIImage *listIcon = [FLEXResources listIcon];
  38. self.hierarchyItem = [FLEXToolbarItem toolbarItemWithTitle:@"views" image:listIcon];
  39. UIImage *selectIcon = [FLEXResources selectIcon];
  40. self.selectItem = [FLEXToolbarItem toolbarItemWithTitle:@"select" image:selectIcon];
  41. UIImage *moveIcon = [FLEXResources moveIcon];
  42. self.moveItem = [FLEXToolbarItem toolbarItemWithTitle:@"move" image:moveIcon];
  43. UIImage *closeIcon = [FLEXResources closeIcon];
  44. self.closeItem = [FLEXToolbarItem toolbarItemWithTitle:@"close" image:closeIcon];
  45. self.backgroundColor = [UIColor clearColor];
  46. self.selectedViewDescriptionContainer = [[UIView alloc] init];
  47. self.selectedViewDescriptionContainer.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.95];
  48. self.selectedViewDescriptionContainer.hidden = YES;
  49. [self addSubview:self.selectedViewDescriptionContainer];
  50. self.selectedViewColorIndicator = [[UIView alloc] init];
  51. self.selectedViewColorIndicator.backgroundColor = [UIColor redColor];
  52. [self.selectedViewDescriptionContainer addSubview:self.selectedViewColorIndicator];
  53. self.selectedViewDescriptionLabel = [[UILabel alloc] init];
  54. self.selectedViewDescriptionLabel.backgroundColor = [UIColor clearColor];
  55. self.selectedViewDescriptionLabel.font = [[self class] descriptionLabelFont];
  56. [self.selectedViewDescriptionContainer addSubview:self.selectedViewDescriptionLabel];
  57. self.toolbarItems = @[_globalsItem, _hierarchyItem, _selectItem, _moveItem, _closeItem];
  58. }
  59. return self;
  60. }
  61. - (void)layoutSubviews
  62. {
  63. [super layoutSubviews];
  64. // Drag Handle
  65. const CGFloat kToolbarItemHeight = [[self class] toolbarItemHeight];
  66. self.dragHandle.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, [[self class] dragHandleWidth], kToolbarItemHeight);
  67. CGRect dragHandleImageFrame = self.dragHandleImageView.frame;
  68. dragHandleImageFrame.origin.x = FLEXFloor((self.dragHandle.frame.size.width - dragHandleImageFrame.size.width) / 2.0);
  69. dragHandleImageFrame.origin.y = FLEXFloor((self.dragHandle.frame.size.height - dragHandleImageFrame.size.height) / 2.0);
  70. self.dragHandleImageView.frame = dragHandleImageFrame;
  71. // Toolbar Items
  72. CGFloat originX = CGRectGetMaxX(self.dragHandle.frame);
  73. CGFloat originY = self.bounds.origin.y;
  74. CGFloat height = kToolbarItemHeight;
  75. CGFloat width = FLEXFloor((CGRectGetMaxX(self.bounds) - originX) / [self.toolbarItems count]);
  76. for (UIView *toolbarItem in self.toolbarItems) {
  77. toolbarItem.frame = CGRectMake(originX, originY, width, height);
  78. originX = CGRectGetMaxX(toolbarItem.frame);
  79. }
  80. // Make sure the last toolbar item goes to the edge to account for any accumulated rounding effects.
  81. UIView *lastToolbarItem = [self.toolbarItems lastObject];
  82. CGRect lastToolbarItemFrame = lastToolbarItem.frame;
  83. lastToolbarItemFrame.size.width = CGRectGetMaxX(self.bounds) - lastToolbarItemFrame.origin.x;
  84. lastToolbarItem.frame = lastToolbarItemFrame;
  85. const CGFloat kSelectedViewColorDiameter = [[self class] selectedViewColorIndicatorDiameter];
  86. const CGFloat kDescriptionLabelHeight = [[self class] descriptionLabelHeight];
  87. const CGFloat kHorizontalPadding = [[self class] horizontalPadding];
  88. const CGFloat kDescriptionVerticalPadding = [[self class] descriptionVerticalPadding];
  89. const CGFloat kDescriptionContainerHeight = [[self class] descriptionContainerHeight];
  90. CGRect descriptionContainerFrame = CGRectZero;
  91. descriptionContainerFrame.size.height = kDescriptionContainerHeight;
  92. descriptionContainerFrame.origin.y = CGRectGetMaxY(self.bounds) - kDescriptionContainerHeight;
  93. descriptionContainerFrame.size.width = self.bounds.size.width;
  94. self.selectedViewDescriptionContainer.frame = descriptionContainerFrame;
  95. // Selected View Color
  96. CGRect selectedViewColorFrame = CGRectZero;
  97. selectedViewColorFrame.size.width = kSelectedViewColorDiameter;
  98. selectedViewColorFrame.size.height = kSelectedViewColorDiameter;
  99. selectedViewColorFrame.origin.x = kHorizontalPadding;
  100. selectedViewColorFrame.origin.y = FLEXFloor((kDescriptionContainerHeight - kSelectedViewColorDiameter) / 2.0);
  101. self.selectedViewColorIndicator.frame = selectedViewColorFrame;
  102. self.selectedViewColorIndicator.layer.cornerRadius = ceil(selectedViewColorFrame.size.height / 2.0);
  103. // Selected View Description
  104. CGRect descriptionLabelFrame = CGRectZero;
  105. CGFloat descriptionOriginX = CGRectGetMaxX(selectedViewColorFrame) + kHorizontalPadding;
  106. descriptionLabelFrame.size.height = kDescriptionLabelHeight;
  107. descriptionLabelFrame.origin.x = descriptionOriginX;
  108. descriptionLabelFrame.origin.y = kDescriptionVerticalPadding;
  109. descriptionLabelFrame.size.width = CGRectGetMaxX(self.selectedViewDescriptionContainer.bounds) - kHorizontalPadding - descriptionOriginX;
  110. self.selectedViewDescriptionLabel.frame = descriptionLabelFrame;
  111. }
  112. #pragma mark - Setter Overrides
  113. - (void)setToolbarItems:(NSArray *)toolbarItems {
  114. if (_toolbarItems == toolbarItems) {
  115. return;
  116. }
  117. // Remove old toolbar items, if any
  118. for (FLEXToolbarItem *item in _toolbarItems) {
  119. [item removeFromSuperview];
  120. }
  121. // Trim to 5 items if necessary
  122. if (toolbarItems.count > 5) {
  123. toolbarItems = [toolbarItems subarrayWithRange:NSMakeRange(0, 5)];
  124. }
  125. for (FLEXToolbarItem *item in toolbarItems) {
  126. [self addSubview:item];
  127. }
  128. _toolbarItems = toolbarItems.copy;
  129. // Lay out new items
  130. [self setNeedsLayout];
  131. [self layoutIfNeeded];
  132. }
  133. - (void)setSelectedViewOverlayColor:(UIColor *)selectedViewOverlayColor
  134. {
  135. if (![_selectedViewOverlayColor isEqual:selectedViewOverlayColor]) {
  136. _selectedViewOverlayColor = selectedViewOverlayColor;
  137. self.selectedViewColorIndicator.backgroundColor = selectedViewOverlayColor;
  138. }
  139. }
  140. - (void)setSelectedViewDescription:(NSString *)selectedViewDescription
  141. {
  142. if (![_selectedViewDescription isEqual:selectedViewDescription]) {
  143. _selectedViewDescription = selectedViewDescription;
  144. self.selectedViewDescriptionLabel.text = selectedViewDescription;
  145. BOOL showDescription = [selectedViewDescription length] > 0;
  146. self.selectedViewDescriptionContainer.hidden = !showDescription;
  147. }
  148. }
  149. #pragma mark - Sizing Convenience Methods
  150. + (UIFont *)descriptionLabelFont
  151. {
  152. return [UIFont systemFontOfSize:12.0];
  153. }
  154. + (CGFloat)toolbarItemHeight
  155. {
  156. return 44.0;
  157. }
  158. + (CGFloat)dragHandleWidth
  159. {
  160. return 30.0;
  161. }
  162. + (CGFloat)descriptionLabelHeight
  163. {
  164. return ceil([[self descriptionLabelFont] lineHeight]);
  165. }
  166. + (CGFloat)descriptionVerticalPadding
  167. {
  168. return 2.0;
  169. }
  170. + (CGFloat)descriptionContainerHeight
  171. {
  172. return [self descriptionVerticalPadding] * 2.0 + [self descriptionLabelHeight];
  173. }
  174. + (CGFloat)selectedViewColorIndicatorDiameter
  175. {
  176. return ceil([self descriptionLabelHeight] / 2.0);
  177. }
  178. + (CGFloat)horizontalPadding
  179. {
  180. return 11.0;
  181. }
  182. - (CGSize)sizeThatFits:(CGSize)size
  183. {
  184. CGFloat height = 0.0;
  185. height += [[self class] toolbarItemHeight];
  186. height += [[self class] descriptionContainerHeight];
  187. return CGSizeMake(size.width, height);
  188. }
  189. @end