PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/UICatalog/ViewControllers/ControlsViewController.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 414 lines | 262 code | 84 blank | 68 comment | 24 complexity | 4fa14478008d88d6fe2fa9d649dad718 MD5 | raw file
  1. /*
  2. File: ControlsViewController.m
  3. Abstract: The view controller for hosting the UIControls features of this sample.
  4. Version: 2.11
  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) 2013 Apple Inc. All Rights Reserved.
  40. */
  41. #import "ControlsViewController.h"
  42. #import "Constants.h"
  43. #define kSliderHeight 7.0
  44. #define kProgressIndicatorSize 40.0
  45. #define kUIProgressBarWidth 160.0
  46. #define kUIProgressBarHeight 24.0
  47. #define kViewTag 1 // for tagging our embedded controls for removal at cell recycle time
  48. static NSString *kSectionTitleKey = @"sectionTitleKey";
  49. static NSString *kLabelKey = @"labelKey";
  50. static NSString *kSourceKey = @"sourceKey";
  51. static NSString *kViewKey = @"viewKey";
  52. // tableView cell id constants
  53. static NSString *kDisplayCellID = @"DisplayCellID";
  54. static NSString *kSourceCellID = @"SourceCellID";
  55. #pragma mark -
  56. @interface ControlsViewController ()
  57. @property (nonatomic, strong) UISwitch *switchCtl;
  58. @property (nonatomic, strong) UISlider *sliderCtl;
  59. @property (nonatomic, strong) UISlider *customSlider;
  60. @property (nonatomic, strong) UIPageControl *pageControl;
  61. @property (nonatomic, strong) UIActivityIndicatorView *progressInd;
  62. @property (nonatomic, strong) UIColor *progressIndSavedColor;
  63. @property (nonatomic, strong) UIProgressView *progressBar;
  64. @property (nonatomic, strong) UIStepper *stepper;
  65. @property (nonatomic, strong) NSArray *dataSourceArray;
  66. @end
  67. #pragma mark -
  68. @implementation ControlsViewController
  69. - (void)viewDidLoad
  70. {
  71. [super viewDidLoad];
  72. self.title = NSLocalizedString(@"ControlsTitle", @"");
  73. self.dataSourceArray = @[
  74. @{ kSectionTitleKey:@"UISwitch",
  75. kLabelKey:@"Standard Switch",
  76. kSourceKey:@"ControlsViewController.m:\r-(UISwitch *)switchCtl",
  77. kViewKey:self.switchCtl },
  78. @{ kSectionTitleKey:@"UISlider",
  79. kLabelKey:@"Standard Slider",
  80. kSourceKey:@"ControlsViewController.m:\r-(UISlider *)sliderCtl",
  81. kViewKey:self.sliderCtl },
  82. @{ kSectionTitleKey:@"UISlider",
  83. kLabelKey:@"Customized Slider",
  84. kSourceKey:@"ControlsViewController.m:\r-(UISlider *)customSlider",
  85. kViewKey:self.customSlider },
  86. @{ kSectionTitleKey:@"UIPageControl",
  87. kLabelKey:@"Ten Pages",
  88. kSourceKey:@"ControlsViewController.m:\r-(UIPageControl *)pageControl",
  89. kViewKey:self.pageControl },
  90. @{ kSectionTitleKey:@"UIActivityIndicatorView",
  91. kLabelKey:@"Style Gray",
  92. kSourceKey:@"ControlsViewController.m:\r-(UIActivityIndicatorView *)progressInd",
  93. kViewKey:self.progressInd },
  94. @{ kSectionTitleKey:@"UIProgressView",
  95. kLabelKey:@"Style Default",
  96. kSourceKey:@"ControlsViewController.m:\r-(UIProgressView *)progressBar",
  97. kViewKey:self.progressBar },
  98. @{ kSectionTitleKey:@"UIStepper",
  99. kLabelKey:@"Stepper 1 to 10",
  100. kSourceKey:@"ControlsViewController.m:\r-(UIStepper *)stepper",
  101. kViewKey:self.stepper }
  102. ];
  103. // add tint bar button
  104. UIBarButtonItem *tintButton = [[UIBarButtonItem alloc] initWithTitle:@"Tinted"
  105. style:UIBarButtonItemStyleBordered
  106. target:self
  107. action:@selector(tintAction:)];
  108. self.navigationItem.rightBarButtonItem = tintButton;
  109. // register our cell IDs for later when we are asked for UITableViewCells
  110. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kDisplayCellID];
  111. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kSourceCellID];
  112. }
  113. #pragma mark - UITableViewDataSource
  114. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  115. {
  116. return [self.dataSourceArray count];
  117. }
  118. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  119. {
  120. return [[self.dataSourceArray objectAtIndex: section] valueForKey:kSectionTitleKey];
  121. }
  122. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  123. {
  124. return 2;
  125. }
  126. // to determine specific row height for each cell, override this.
  127. // In this example, each row is determined by its subviews that are embedded.
  128. //
  129. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. return ([indexPath row] == 0) ? 50.0 : 38.0;
  132. }
  133. // to determine which UITableViewCell to be used on a given row.
  134. //
  135. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. UITableViewCell *cell = nil;
  138. if ([indexPath row] == 0)
  139. {
  140. cell = [tableView dequeueReusableCellWithIdentifier:kDisplayCellID forIndexPath:indexPath];
  141. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  142. UIView *viewToRemove = nil;
  143. viewToRemove = [cell.contentView viewWithTag:kViewTag];
  144. if (viewToRemove)
  145. [viewToRemove removeFromSuperview];
  146. cell.textLabel.text = [[self.dataSourceArray objectAtIndex:indexPath.section] valueForKey:kLabelKey];
  147. UIControl *control = [[self.dataSourceArray objectAtIndex:indexPath.section] valueForKey:kViewKey];
  148. // make sure this control is right-justified to the right side of the cell
  149. CGRect newFrame = control.frame;
  150. newFrame.origin.x = CGRectGetWidth(cell.contentView.frame) - CGRectGetWidth(newFrame) - 10.0;
  151. control.frame = newFrame;
  152. // if the cell is ever resized, keep the control over to the right
  153. control.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  154. [cell.contentView addSubview:control];
  155. if (control == (UIControl *)self.progressInd)
  156. {
  157. [self.progressInd startAnimating]; // UIActivityIndicatorView needs to re-animate
  158. }
  159. }
  160. else
  161. {
  162. cell = [tableView dequeueReusableCellWithIdentifier:kSourceCellID forIndexPath:indexPath];
  163. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  164. cell.textLabel.opaque = NO;
  165. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  166. cell.textLabel.textColor = [UIColor grayColor];
  167. cell.textLabel.numberOfLines = 2;
  168. cell.textLabel.highlightedTextColor = [UIColor blackColor];
  169. cell.textLabel.font = [UIFont systemFontOfSize:12.0];
  170. cell.textLabel.text = [[self.dataSourceArray objectAtIndex:indexPath.section] valueForKey:kSourceKey];
  171. }
  172. return cell;
  173. }
  174. - (void)switchAction:(id)sender
  175. {
  176. // NSLog(@"switchAction: value = %d", [sender isOn]);
  177. }
  178. - (void)pageAction:(id)sender
  179. {
  180. // NSLog(@"pageAction: current page = %d", [sender currentPage]);
  181. }
  182. - (void)sliderAction:(id)sender
  183. {
  184. // UISlider *slider = (UISlider *)sender;
  185. // NSLog(@"sliderAction: value = %f", [slider value]);
  186. }
  187. - (void)stepperAction:(id)sender
  188. {
  189. // UIStepper *actualStepper = (UIStepper *)sender;
  190. // NSLog(@"stepperAction: value = %f", [actualStepper value]);
  191. }
  192. #pragma mark - Lazy creation of controls
  193. - (UISwitch *)switchCtl
  194. {
  195. if (_switchCtl == nil)
  196. {
  197. CGRect frame = CGRectMake(0.0, 12.0, 94.0, 27.0);
  198. _switchCtl = [[UISwitch alloc] initWithFrame:frame];
  199. [_switchCtl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
  200. // in case the parent view draws with a custom color or gradient, use a transparent color
  201. _switchCtl.backgroundColor = [UIColor clearColor];
  202. [_switchCtl setAccessibilityLabel:NSLocalizedString(@"StandardSwitch", @"")];
  203. _switchCtl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  204. }
  205. return _switchCtl;
  206. }
  207. - (UISlider *)sliderCtl
  208. {
  209. if (_sliderCtl == nil)
  210. {
  211. CGRect frame = CGRectMake(0.0, 12.0, 120.0, kSliderHeight);
  212. _sliderCtl = [[UISlider alloc] initWithFrame:frame];
  213. [_sliderCtl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
  214. // in case the parent view draws with a custom color or gradient, use a transparent color
  215. _sliderCtl.backgroundColor = [UIColor clearColor];
  216. _sliderCtl.minimumValue = 0.0;
  217. _sliderCtl.maximumValue = 100.0;
  218. _sliderCtl.continuous = YES;
  219. _sliderCtl.value = 50.0;
  220. // Add an accessibility label that describes the slider.
  221. [_sliderCtl setAccessibilityLabel:NSLocalizedString(@"StandardSlider", @"")];
  222. _sliderCtl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  223. }
  224. return _sliderCtl;
  225. }
  226. - (UISlider *)customSlider
  227. {
  228. if (_customSlider == nil)
  229. {
  230. CGRect frame = CGRectMake(0.0, 12.0, 120.0, kSliderHeight);
  231. _customSlider = [[UISlider alloc] initWithFrame:frame];
  232. [_customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
  233. // in case the parent view draws with a custom color or gradient, use a transparent color
  234. _customSlider.backgroundColor = [UIColor clearColor];
  235. UIImage *stetchLeftTrack = [[UIImage imageNamed:@"orangeslide.png"]
  236. stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
  237. UIImage *stetchRightTrack = [[UIImage imageNamed:@"yellowslide.png"]
  238. stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
  239. [_customSlider setThumbImage: [UIImage imageNamed:@"slider_ball.png"] forState:UIControlStateNormal];
  240. [_customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
  241. [_customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
  242. _customSlider.minimumValue = 0.0;
  243. _customSlider.maximumValue = 100.0;
  244. _customSlider.continuous = YES;
  245. _customSlider.value = 50.0;
  246. // Add an accessibility label that describes the slider.
  247. [_customSlider setAccessibilityLabel:NSLocalizedString(@"CustomSlider", @"")];
  248. _customSlider.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  249. }
  250. return _customSlider;
  251. }
  252. - (UIPageControl *)pageControl
  253. {
  254. if (_pageControl == nil)
  255. {
  256. CGRect frame = CGRectMake(0.0, 14.0, 178.0, 20.0);
  257. _pageControl = [[UIPageControl alloc] initWithFrame:frame];
  258. [_pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
  259. // in case the parent view draws with a custom color or gradient, use a transparent color
  260. _pageControl.backgroundColor = [UIColor grayColor];
  261. _pageControl.numberOfPages = 10; // must be set or control won't draw
  262. _pageControl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  263. }
  264. return _pageControl;
  265. }
  266. - (UIActivityIndicatorView *)progressInd
  267. {
  268. if (_progressInd == nil)
  269. {
  270. CGRect frame = CGRectMake(0.0, 12.0, kProgressIndicatorSize, kProgressIndicatorSize);
  271. _progressInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  272. self.progressIndSavedColor = _progressInd.color;
  273. _progressInd.frame = frame;
  274. [_progressInd sizeToFit];
  275. _progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
  276. _progressInd.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  277. [_progressInd startAnimating];
  278. }
  279. return _progressInd;
  280. }
  281. - (UIProgressView *)progressBar
  282. {
  283. if (_progressBar == nil)
  284. {
  285. CGRect frame = CGRectMake(0.0, 20.0, kUIProgressBarWidth, kUIProgressBarHeight);
  286. _progressBar = [[UIProgressView alloc] initWithFrame:frame];
  287. _progressBar.progressViewStyle = UIProgressViewStyleDefault;
  288. _progressBar.progress = 0.5;
  289. _progressBar.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  290. }
  291. return _progressBar;
  292. }
  293. - (UIStepper *)stepper
  294. {
  295. if (_stepper == nil && [UIStepper class])
  296. {
  297. CGRect frame = CGRectMake(0.0, 10.0, 0.0, 0.0);
  298. _stepper = [[UIStepper alloc] initWithFrame:frame];
  299. [_stepper sizeToFit]; // size the control to it's normal size
  300. _stepper.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  301. _stepper.value = 0;
  302. _stepper.minimumValue = 0;
  303. _stepper.maximumValue = 10;
  304. _stepper.stepValue = 1;
  305. [_stepper addTarget:self action:@selector(stepperAction:) forControlEvents:UIControlEventValueChanged];
  306. }
  307. return _stepper;
  308. }
  309. - (void)tintAction:(id)sender
  310. {
  311. UIColor *tintColor = (self.progressBar.progressTintColor != nil) ? nil : [UIColor blueColor];
  312. self.progressBar.progressTintColor = tintColor;
  313. self.progressBar.trackTintColor = tintColor;
  314. self.sliderCtl.minimumTrackTintColor = tintColor;
  315. self.sliderCtl.thumbTintColor = tintColor;
  316. self.switchCtl.onTintColor = tintColor;
  317. self.stepper.tintColor = tintColor;
  318. UIColor *thumbTintColor = (self.switchCtl.thumbTintColor != nil) ? nil : [UIColor redColor];
  319. self.switchCtl.onTintColor = tintColor;
  320. self.switchCtl.thumbTintColor = thumbTintColor;
  321. UIColor *progressIndColor = (self.progressInd.color != self.progressIndSavedColor) ? self.progressIndSavedColor : [UIColor blueColor];
  322. self.progressInd.color = progressIndColor;
  323. }
  324. @end