FLEXExplorerToolbar.m 8.8 KB

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