SwatchesView.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "SwatchesView.h"
  19. static CGFloat kSwatchButtonHeightPhone = 85.0f;
  20. static CGFloat kSwatchButtonWidthPhone = 30.0f;
  21. static CGFloat kSwatchButtonHeightPad= 166.0f;
  22. static CGFloat kSwatchButtonWidthPad = 57.0f;
  23. static CGFloat kSwatchPencilPadding = 1.0f;
  24. @interface SwatchesView()
  25. @property (nonatomic, strong) UIImageView *selectedIconView;
  26. @property (nonatomic, strong) NSDictionary *colors;
  27. @property (nonatomic, strong) NSArray *colorButtons;
  28. @end
  29. @implementation SwatchesView
  30. - (instancetype)initWithFrame:(CGRect)frame
  31. {
  32. frame.size.height = [SwatchesView sizeForDevice].height;
  33. if (self = [super initWithFrame:frame]) {
  34. [self setupButtons];
  35. }
  36. return self;
  37. }
  38. - (void)setupButtons
  39. {
  40. self.colors = [UIColor realmColors];
  41. NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:self.colors.count];
  42. NSInteger tag = 0;
  43. for (NSString *color in self.colors.allKeys) {
  44. UIImage *pencilImage = [[UIImage imageNamed:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  45. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  46. button.tag = tag++;
  47. button.contentMode = UIViewContentModeScaleAspectFit;
  48. [button setImage:pencilImage forState:UIControlStateNormal];
  49. [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  50. [self addSubview:button];
  51. [buttons addObject:button];
  52. }
  53. self.colorButtons = buttons;
  54. CGSize swatchSize = [SwatchesView sizeForDevice];
  55. CGFloat totalWidth = (self.colors.count * swatchSize.width) + ((self.colors.count-1) * kSwatchPencilPadding);
  56. CGFloat x = 0.0f;
  57. for (UIButton *button in self.colorButtons) {
  58. CGRect frame = button.frame;
  59. frame.origin.x = x;
  60. frame.size = swatchSize;
  61. button.frame = frame;
  62. x += swatchSize.width + kSwatchPencilPadding;
  63. }
  64. self.contentSize = (CGSize){totalWidth, swatchSize.height};
  65. [self updateContentInset];
  66. self.selectedIconView = [[UIImageView alloc] initWithImage:[[self class] circleIcon]];
  67. [self addSelectedIconToButton:self.colorButtons.firstObject];
  68. }
  69. - (void)setFrame:(CGRect)frame
  70. {
  71. [super setFrame:frame];
  72. [self updateContentInset];
  73. }
  74. - (void)updateContentInset
  75. {
  76. CGSize contentSize = self.contentSize;
  77. CGSize size = self.frame.size;
  78. UIEdgeInsets contentInsets = UIEdgeInsetsZero;
  79. //Only do content insets if the scroll size is smaller than the window
  80. if (contentSize.width < size.width) {
  81. CGFloat inset = (size.width - contentSize.width) * 0.5f;
  82. contentInsets.left = inset;
  83. contentInsets.right = inset;
  84. }
  85. self.contentInset = contentInsets;
  86. }
  87. - (void)addSelectedIconToButton:(UIButton *)button
  88. {
  89. [button addSubview:self.selectedIconView];
  90. CGRect frame = self.selectedIconView.frame;
  91. frame.origin.x = (button.frame.size.width - frame.size.width) * 0.5f;
  92. frame.origin.y = button.frame.size.height - 12.0f;
  93. self.selectedIconView.frame = frame;
  94. }
  95. - (void)buttonTapped:(id)sender
  96. {
  97. UIButton *button = (UIButton *)sender;
  98. self.selectedColor = self.colors.allKeys[button.tag];
  99. [self addSelectedIconToButton:sender];
  100. }
  101. - (void)setSelectedColor:(NSString *)selectedColor
  102. {
  103. if (selectedColor == _selectedColor) {
  104. return;
  105. }
  106. _selectedColor = selectedColor;
  107. if (self.swatchColorChangedHandler)
  108. self.swatchColorChangedHandler();
  109. }
  110. + (CGSize)sizeForDevice
  111. {
  112. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  113. return (CGSize){kSwatchButtonWidthPad, kSwatchButtonHeightPad};
  114. }
  115. return (CGSize){kSwatchButtonWidthPhone, kSwatchButtonHeightPhone};
  116. }
  117. + (UIImage *)circleIcon
  118. {
  119. CGRect rect = CGRectMake(0, 0, 6.0f, 6.0f);
  120. UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);
  121. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
  122. [[UIColor colorWithWhite:1.0f alpha:0.8f] set];
  123. [path fill];
  124. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  125. UIGraphicsEndImageContext();
  126. return image;
  127. }
  128. @end