FLEXExplorerToolbar.m 8.7 KB

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