TiUISliderNew.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // TiUISliderNew.m
  3. // TiSDKDemo
  4. //
  5. // Created by iMacA1002 on 2019/12/2.
  6. // Copyright © 2020 Tillusory Tech. All rights reserved.
  7. //
  8. #import "TiUISliderNew.h"
  9. #import "TIConfig.h"
  10. @interface TiUISliderNew (){
  11. CGRect _trackRect;
  12. TiUISliderType _sliderType;
  13. }
  14. //滑动的标记View
  15. @property(nonatomic,strong)UIImageView *tagView;
  16. @property(nonatomic,strong)UILabel *tagLabel;
  17. //覆盖trackmax的线
  18. @property(nonatomic,strong)UIView *trackColorView;
  19. //用于标记分割的线
  20. @property(nonatomic,strong)UIView *tagLine;
  21. @end
  22. @implementation TiUISliderNew
  23. -(UIImageView *)tagView{
  24. if (_tagView == nil) {
  25. // (TiUISliderHeight*3+2)为滑块直径 TiUISliderHeight/2 为滑条半经
  26. _tagView = [[UIImageView alloc]initWithFrame:CGRectMake(-TiUISliderTagViewWidth/2+1, -(TiUISliderTagViewHeight + (TiUISliderHeight*3+2)/2 - TiUISliderHeight/2),TiUISliderTagViewWidth, TiUISliderTagViewHeight)];
  27. [_tagView setImage:[UIImage imageNamed:@"drag.png"]];
  28. _tagView.alpha = 0;
  29. _tagView.contentMode = UIViewContentModeScaleAspectFit;
  30. [_tagView addSubview:self.tagLabel];
  31. }
  32. return _tagView;
  33. }
  34. -(UILabel *)tagLabel{
  35. if (_tagLabel==nil) {
  36. _tagLabel = [[UILabel alloc] initWithFrame:self.tagView.bounds];
  37. [_tagLabel setTextColor:TI_Color_Default_Text_White];
  38. [_tagLabel setTextAlignment:NSTextAlignmentCenter];
  39. [_tagLabel setFont:TI_Font_Default_Size_Small];
  40. _tagLabel.userInteractionEnabled = NO;
  41. }
  42. return _tagLabel;
  43. }
  44. - (UIView *)trackColorView{
  45. if (!_trackColorView) {
  46. _trackColorView = [[UIView alloc] init];
  47. _trackColorView.frame = _trackRect;
  48. _trackColorView.backgroundColor = [UIColor colorWithRed:239/255.0 green:128/255.0 blue:116/255.0 alpha:1.0];
  49. // _trackColorView.backgroundColor = [UIColor yellowColor];
  50. _trackColorView.layer.cornerRadius = TiUISliderHeight/2;
  51. _trackColorView.userInteractionEnabled = NO;
  52. }
  53. return _trackColorView;
  54. }
  55. - (UIView *)tagLine{
  56. if (!_tagLine) {
  57. _tagLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, TiUISliderHeight*3)];
  58. _tagLine.hidden = YES;
  59. _tagLine.backgroundColor = TI_Color_Default_Text_White;
  60. _tagLine.userInteractionEnabled = NO;
  61. _tagLine.layer.cornerRadius = 0.5;
  62. }
  63. return _tagLine;
  64. }
  65. - (instancetype)init{
  66. self = [super init];
  67. if (self) {
  68. _trackRect = CGRectZero;
  69. //默认赋值
  70. // [self setSliderType:TI_UI_SLIDER_TYPE_ONE WithValue:0];
  71. [self setBackgroundColor:[UIColor colorWithRed:255/255.0 green:254/255.0 blue:252/255.0 alpha:1.0]];
  72. self.minimumTrackTintColor = [UIColor clearColor];
  73. self.maximumTrackTintColor = [UIColor clearColor];
  74. [self setThumbImage:[self resizeImage:[UIImage imageNamed:@"dot"] toSize:CGSizeMake(TiUISliderHeight*3, TiUISliderHeight*3)] forState:UIControlStateNormal];
  75. [self setThumbImage:[self resizeImage:[UIImage imageNamed:@"dot"] toSize:CGSizeMake(TiUISliderHeight*3+2, TiUISliderHeight*3+2)] forState:UIControlStateHighlighted];
  76. self.layer.cornerRadius = TiUISliderHeight/2;
  77. [self addSubview:self.tagView];
  78. [self addSubview:self.trackColorView];
  79. [self addSubview:self.tagLine];
  80. [self addTarget:self action:@selector(didBeginUpdateValue:) forControlEvents:UIControlEventTouchDown];
  81. [self addTarget:self action:@selector(didUpdateValue:) forControlEvents:UIControlEventValueChanged];
  82. [self addTarget:self action:@selector(didEndUpdateValue:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside|UIControlEventTouchCancel];
  83. }
  84. return self;
  85. }
  86. -(void)setSliderType:(TiUISliderType)sliderType WithValue:(float)value{
  87. _sliderType = sliderType;
  88. [self refreshWithValue:value isSet:YES];
  89. if (sliderType == TI_UI_SLIDER_TYPE_ONE)
  90. {
  91. self.tagLine.hidden = YES;
  92. self.minimumValue = 0;
  93. self.maximumValue = 100;
  94. [self setValue:value animated:YES];
  95. }
  96. else if (sliderType == TI_UI_SLIDER_TYPE_TWO)
  97. {
  98. self.tagLine.hidden = NO;
  99. self.minimumValue = -50;
  100. self.maximumValue = 50;
  101. [self setValue:value animated:YES];
  102. }
  103. }
  104. //开始拖拽
  105. - (void)didBeginUpdateValue:(UISlider *)sender {
  106. [self refreshWithValue:sender.value isSet:NO];
  107. [UIView animateWithDuration:0.3 animations:^{
  108. [self.tagView setAlpha:1.0f];
  109. }];
  110. }
  111. //正在拖拽
  112. - (void)didUpdateValue:(UISlider *)sender {
  113. [self refreshWithValue:sender.value isSet:NO];
  114. // [self.tagView setAlpha:1.0f];
  115. }
  116. //结束拖拽
  117. - (void)didEndUpdateValue:(UISlider *)sender {
  118. [self refreshWithValue:sender.value isSet:NO];
  119. [UIView animateWithDuration:0.1 animations:^{
  120. [self.tagView setAlpha:0];
  121. }];
  122. }
  123. - (void)refreshWithValue:(float)value isSet:(BOOL)set{
  124. if (self.refreshValueBlock&&!set) {
  125. self.refreshValueBlock(value);
  126. }
  127. if(self.valueBlock){
  128. self.valueBlock(value);
  129. }
  130. if (self->_sliderType == TI_UI_SLIDER_TYPE_ONE)
  131. {
  132. self.trackColorView.frame =CGRectMake(0, 0, self->_trackRect.origin.x + TiUISliderHeight*3/2, TiUISliderHeight);
  133. }
  134. else if (self->_sliderType == TI_UI_SLIDER_TYPE_TWO)
  135. {
  136. CGFloat W = -(self.frame.size.width/2 - (self->_trackRect.origin.x + TiUISliderHeight*3/2));
  137. self.trackColorView.frame =CGRectMake(self.frame.size.width/2 +0.5 , 0,W , TiUISliderHeight);
  138. }
  139. self.tagView.center = CGPointMake(self->_trackRect.origin.x + (TiUISliderHeight*3)/2 +1,self.tagView.center.y);
  140. [self.tagLabel setText:[NSString stringWithFormat:@"%d%@", (int)value, @"%"]];
  141. }
  142. //调整中间滑块位置,并获取滑块坐标
  143. - (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
  144. rect.origin.x = rect.origin.x - TiUISliderHeight;
  145. rect.size.width = rect.size.width + TiUISliderHeight*2;
  146. _trackRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
  147. return CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], TiUISliderHeight, TiUISliderHeight);
  148. }
  149. // FIXME: --layoutSubviews--
  150. -(void)layoutSubviews
  151. {
  152. [super layoutSubviews];
  153. //使用 mas //这里才能获取到self.frame 并且刷新Value 视图变动的时候也会调用
  154. self.tagLine.frame = CGRectMake(self.frame.size.width/2, -TiUISliderHeight*3/2 + TiUISliderHeight/2, 1, TiUISliderHeight*3);
  155. [self refreshWithValue:self.value isSet:YES];
  156. }
  157. - (UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)size{
  158. UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
  159. // 绘制改变大小的图片
  160. [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  161. // 从当前context中创建一个改变大小后的图片
  162. UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  163. // 使当前的context出堆栈
  164. UIGraphicsEndImageContext();
  165. // 返回新的改变大小后的图片
  166. return scaledImage;
  167. }
  168. @end