AAPLPickerViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. File: AAPLPickerViewController.m
  3. Abstract: A view controller that demonstrates how to use UIPickerView.
  4. Version: 2.12
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  6. Inc. ("Apple") in consideration of your agreement to the following
  7. terms, and your use, installation, modification or redistribution of
  8. this Apple software constitutes acceptance of these terms. If you do
  9. not agree with these terms, please do not use, install, modify or
  10. redistribute this Apple software.
  11. In consideration of your agreement to abide by the following terms, and
  12. subject to these terms, Apple grants you a personal, non-exclusive
  13. license, under Apple's copyrights in this original Apple software (the
  14. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  15. Software, with or without modifications, in source and/or binary forms;
  16. provided that if you redistribute the Apple Software in its entirety and
  17. without modifications, you must retain this notice and the following
  18. text and disclaimers in all such redistributions of the Apple Software.
  19. Neither the name, trademarks, service marks or logos of Apple Inc. may
  20. be used to endorse or promote products derived from the Apple Software
  21. without specific prior written permission from Apple. Except as
  22. expressly stated in this notice, no other rights or licenses, express or
  23. implied, are granted by Apple herein, including but not limited to any
  24. patent rights that may be infringed by your derivative works or by other
  25. works in which the Apple Software may be incorporated.
  26. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  27. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  28. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  29. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  30. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  31. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  32. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  35. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  36. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  37. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  38. POSSIBILITY OF SUCH DAMAGE.
  39. Copyright (C) 2014 Apple Inc. All Rights Reserved.
  40. */
  41. #import "AAPLPickerViewController.h"
  42. typedef NS_ENUM(NSInteger, AAPLPickerViewControllerColorComponent) {
  43. AAPLColorComponentRed = 0,
  44. AAPLColorComponentGreen,
  45. AAPLColorComponentBlue,
  46. AAPLColorComponentCount
  47. };
  48. // The maximum RGB color
  49. #define AAPL_RGB_MAX 255.0
  50. @interface AAPLPickerViewController()<UIPickerViewDataSource, UIPickerViewDelegate, UIPickerViewAccessibilityDelegate>
  51. @property (nonatomic, weak) IBOutlet UIPickerView *pickerView;
  52. @property (nonatomic, weak) IBOutlet UIView *colorSwatchView;
  53. @property (nonatomic) CGFloat redColorComponent;
  54. @property (nonatomic) CGFloat greenColorComponent;
  55. @property (nonatomic) CGFloat blueColorComponent;
  56. @end
  57. #pragma mark -
  58. @implementation AAPLPickerViewController
  59. - (void)viewDidLoad {
  60. [super viewDidLoad];
  61. // Show that a given row is selected. This is off by default.
  62. self.pickerView.showsSelectionIndicator = YES;
  63. [self configurePickerView];
  64. }
  65. // The offset of each color value (from 0 to 255) for red, green, and blue.
  66. - (NSInteger)colorValueOffset {
  67. return 5;
  68. }
  69. - (NSInteger)numberOfColorValuesPerComponent {
  70. return (NSInteger)ceil(AAPL_RGB_MAX / (CGFloat)[self colorValueOffset]) + 1;
  71. }
  72. - (void)updateColorSwatchViewBackgroundColor {
  73. self.colorSwatchView.backgroundColor = [UIColor colorWithRed:self.redColorComponent green:self.greenColorComponent blue:self.blueColorComponent alpha:1];
  74. }
  75. #pragma mark - Configuration
  76. - (void)configurePickerView {
  77. // Set the default selected rows (the desired rows to initially select will vary by use case).
  78. [self selectRowInPickerView:13 withColorComponent:AAPLColorComponentRed];
  79. [self selectRowInPickerView:41 withColorComponent:AAPLColorComponentGreen];
  80. [self selectRowInPickerView:24 withColorComponent:AAPLColorComponentBlue];
  81. }
  82. - (void)selectRowInPickerView:(NSInteger)row withColorComponent:(AAPLPickerViewControllerColorComponent)colorComponent {
  83. // Note that the delegate method on UIPickerViewDelegate is not triggered when manually calling -[UIPickerView selectRow:inComponent:animated:].
  84. // To do this, we fire off the delegate method manually.
  85. [self.pickerView selectRow:row inComponent:(NSInteger)colorComponent animated:YES];
  86. [self pickerView:self.pickerView didSelectRow:row inComponent:(NSInteger)colorComponent];
  87. }
  88. #pragma mark - RGB Color Setter Overrides
  89. - (void)setRedColorComponent:(CGFloat)redColorComponent {
  90. if (_redColorComponent != redColorComponent) {
  91. _redColorComponent = redColorComponent;
  92. [self updateColorSwatchViewBackgroundColor];
  93. }
  94. }
  95. - (void)setGreenColorComponent:(CGFloat)greenColorComponent {
  96. if (_greenColorComponent != greenColorComponent) {
  97. _greenColorComponent = greenColorComponent;
  98. [self updateColorSwatchViewBackgroundColor];
  99. }
  100. }
  101. - (void)setBlueColorComponent:(CGFloat)blueColorComponent {
  102. if (_blueColorComponent != blueColorComponent) {
  103. _blueColorComponent = blueColorComponent;
  104. [self updateColorSwatchViewBackgroundColor];
  105. }
  106. }
  107. #pragma mark - UIPickerViewDataSource
  108. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  109. return AAPLColorComponentCount;
  110. }
  111. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  112. return [self numberOfColorValuesPerComponent];
  113. }
  114. #pragma mark - UIPickerViewDelegate
  115. - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
  116. NSInteger colorValue = row * [self colorValueOffset];
  117. CGFloat colorComponent = (CGFloat)colorValue / AAPL_RGB_MAX;
  118. CGFloat redColorComponent = 0;
  119. CGFloat greenColorComponent = 0;
  120. CGFloat blueColorComponent = 0;
  121. switch (component) {
  122. case AAPLColorComponentRed:
  123. redColorComponent = colorComponent;
  124. break;
  125. case AAPLColorComponentGreen:
  126. greenColorComponent = colorComponent;
  127. break;
  128. case AAPLColorComponentBlue:
  129. blueColorComponent = colorComponent;
  130. break;
  131. default:
  132. NSLog(@"Invalid row/component combination for picker view.");
  133. break;
  134. }
  135. UIColor *foregroundColor = [UIColor colorWithRed:redColorComponent green:greenColorComponent blue:blueColorComponent alpha:1];
  136. NSString *titleText = [NSString stringWithFormat:@"%ld", (long)colorValue];
  137. // Set the foreground color for the attributed string.
  138. NSDictionary *attributes = @{NSForegroundColorAttributeName: foregroundColor};
  139. NSAttributedString *title = [[NSAttributedString alloc] initWithString:titleText attributes:attributes];
  140. return title;
  141. }
  142. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  143. CGFloat colorComponentValue = ((CGFloat)[self colorValueOffset] * row)/AAPL_RGB_MAX;
  144. switch (component) {
  145. case AAPLColorComponentRed:
  146. self.redColorComponent = colorComponentValue;
  147. break;
  148. case AAPLColorComponentGreen:
  149. self.greenColorComponent = colorComponentValue;
  150. break;
  151. case AAPLColorComponentBlue:
  152. self.blueColorComponent = colorComponentValue;
  153. break;
  154. default:
  155. NSLog(@"Invalid row/component combination selected for picker view.");
  156. break;
  157. }
  158. }
  159. #pragma mark - UIPickerViewAccessibilityDelegate
  160. - (NSString *)pickerView:(UIPickerView *)pickerView accessibilityLabelForComponent:(NSInteger)component {
  161. NSString *accessibilityLabel;
  162. switch (component) {
  163. case AAPLColorComponentRed:
  164. accessibilityLabel = NSLocalizedString(@"Red color component value", nil);
  165. break;
  166. case AAPLColorComponentGreen:
  167. accessibilityLabel = NSLocalizedString(@"Green color component value", nil);
  168. break;
  169. case AAPLColorComponentBlue:
  170. accessibilityLabel = NSLocalizedString(@"Blue color component value", nil);
  171. break;
  172. default:
  173. NSLog(@"Invalid row/component combination for picker view.");
  174. break;
  175. }
  176. return accessibilityLabel;
  177. }
  178. @end