FLEXExplorerToolbar.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. const CGFloat kSelectedViewColorDiameter = [[self class] selectedViewColorIndicatorDiameter];
  92. const CGFloat kDescriptionLabelHeight = [[self class] descriptionLabelHeight];
  93. const CGFloat kHorizontalPadding = [[self class] horizontalPadding];
  94. const CGFloat kDescriptionVerticalPadding = [[self class] descriptionVerticalPadding];
  95. const CGFloat kDescriptionContainerHeight = [[self class] descriptionContainerHeight];
  96. CGRect descriptionContainerFrame = CGRectZero;
  97. descriptionContainerFrame.size.height = kDescriptionContainerHeight;
  98. descriptionContainerFrame.origin.y = CGRectGetMaxY(self.bounds) - kDescriptionContainerHeight;
  99. descriptionContainerFrame.size.width = self.bounds.size.width;
  100. self.selectedViewDescriptionContainer.frame = descriptionContainerFrame;
  101. // Selected View Color
  102. CGRect selectedViewColorFrame = CGRectZero;
  103. selectedViewColorFrame.size.width = kSelectedViewColorDiameter;
  104. selectedViewColorFrame.size.height = kSelectedViewColorDiameter;
  105. selectedViewColorFrame.origin.x = kHorizontalPadding;
  106. selectedViewColorFrame.origin.y = floor((kDescriptionContainerHeight - kSelectedViewColorDiameter) / 2.0);
  107. self.selectedViewColorIndicator.frame = selectedViewColorFrame;
  108. self.selectedViewColorIndicator.layer.cornerRadius = ceil(selectedViewColorFrame.size.height / 2.0);
  109. // Selected View Description
  110. CGRect descriptionLabelFrame = CGRectZero;
  111. CGFloat descriptionOriginX = CGRectGetMaxX(selectedViewColorFrame) + kHorizontalPadding;
  112. descriptionLabelFrame.size.height = kDescriptionLabelHeight;
  113. descriptionLabelFrame.origin.x = descriptionOriginX;
  114. descriptionLabelFrame.origin.y = kDescriptionVerticalPadding;
  115. descriptionLabelFrame.size.width = CGRectGetMaxX(self.selectedViewDescriptionContainer.bounds) - kHorizontalPadding - descriptionOriginX;
  116. self.selectedViewDescriptionLabel.frame = descriptionLabelFrame;
  117. }
  118. #pragma mark - Setter Overrides
  119. - (void)setSelectedViewOverlayColor:(UIColor *)selectedViewOverlayColor
  120. {
  121. if (![_selectedViewOverlayColor isEqual:selectedViewOverlayColor]) {
  122. _selectedViewOverlayColor = selectedViewOverlayColor;
  123. self.selectedViewColorIndicator.backgroundColor = selectedViewOverlayColor;
  124. }
  125. }
  126. - (void)setSelectedViewDescription:(NSString *)selectedViewDescription
  127. {
  128. if (![_selectedViewDescription isEqual:selectedViewDescription]) {
  129. _selectedViewDescription = selectedViewDescription;
  130. self.selectedViewDescriptionLabel.text = selectedViewDescription;
  131. BOOL showDescription = [selectedViewDescription length] > 0;
  132. self.selectedViewDescriptionContainer.hidden = !showDescription;
  133. }
  134. }
  135. #pragma mark - Sizing Convenience Methods
  136. + (UIFont *)descriptionLabelFont
  137. {
  138. return [UIFont systemFontOfSize:12.0];
  139. }
  140. + (CGFloat)toolbarItemHeight
  141. {
  142. return 44.0;
  143. }
  144. + (CGFloat)dragHandleWidth
  145. {
  146. return 30.0;
  147. }
  148. + (CGFloat)descriptionLabelHeight
  149. {
  150. return ceil([[self descriptionLabelFont] lineHeight]);
  151. }
  152. + (CGFloat)descriptionVerticalPadding
  153. {
  154. return 2.0;
  155. }
  156. + (CGFloat)descriptionContainerHeight
  157. {
  158. return [self descriptionVerticalPadding] * 2.0 + [self descriptionLabelHeight];
  159. }
  160. + (CGFloat)selectedViewColorIndicatorDiameter
  161. {
  162. return ceil([self descriptionLabelHeight] / 2.0);
  163. }
  164. + (CGFloat)horizontalPadding
  165. {
  166. return 11.0;
  167. }
  168. - (CGSize)sizeThatFits:(CGSize)size
  169. {
  170. CGFloat height = 0.0;
  171. height += [[self class] toolbarItemHeight];
  172. height += [[self class] descriptionContainerHeight];
  173. return CGSizeMake(size.width, height);
  174. }
  175. @end