XLFormSliderCell.m 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // XLFormSliderCell.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "XLFormSliderCell.h"
  26. #import "UIView+XLFormAdditions.h"
  27. @interface XLFormSliderCell ()
  28. @property (nonatomic) UISlider * slider;
  29. @property (nonatomic) UILabel * textLabel;
  30. @property NSUInteger steps;
  31. @end
  32. @implementation XLFormSliderCell
  33. @synthesize textLabel = _textLabel;
  34. - (void)configure
  35. {
  36. self.steps = 0;
  37. [self.slider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  38. [self.contentView addSubview:self.slider];
  39. [self.contentView addSubview:self.textLabel];
  40. self.selectionStyle = UITableViewCellSelectionStyleNone;
  41. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:10]];
  42. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.slider attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:44]];
  43. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[textLabel]-|" options:0 metrics:0 views:@{@"textLabel": self.textLabel}]];
  44. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[slider]-30-|" options:0 metrics:0 views:@{@"slider": self.slider}]];
  45. [self valueChanged:nil];
  46. }
  47. -(void)update {
  48. [super update];
  49. self.textLabel.text = self.rowDescriptor.title;
  50. self.slider.value = [self.rowDescriptor.value floatValue];
  51. self.slider.enabled = !self.rowDescriptor.isDisabled;
  52. [self valueChanged:nil];
  53. }
  54. -(void)valueChanged:(UISlider*)_slider {
  55. if(self.steps != 0) {
  56. self.slider.value = roundf((self.slider.value-self.slider.minimumValue)/(self.slider.maximumValue-self.slider.minimumValue)*self.steps)*(self.slider.maximumValue-self.slider.minimumValue)/self.steps + self.slider.minimumValue;
  57. }
  58. self.rowDescriptor.value = @(self.slider.value);
  59. }
  60. +(CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor {
  61. return 88;
  62. }
  63. -(UILabel *)textLabel
  64. {
  65. if (_textLabel) return _textLabel;
  66. _textLabel = [UILabel autolayoutView];
  67. return _textLabel;
  68. }
  69. -(UISlider *)slider
  70. {
  71. if (_slider) return _slider;
  72. _slider = [UISlider autolayoutView];
  73. return _slider;
  74. }
  75. @end