@@ -74,7 +74,7 @@ @interface DYRulerView : UIView
74
74
@property (nonatomic ,assign )NSInteger betweenNumber;
75
75
@property (nonatomic ,assign )int minValue;
76
76
@property (nonatomic ,assign )int maxValue;
77
- @property (nonatomic ,assign )int step;
77
+ @property (nonatomic ,assign )float step;
78
78
79
79
@end
80
80
@implementation DYRulerView
@@ -94,7 +94,10 @@ -(void)drawRect:(CGRect)rect{
94
94
for (int i = 0 ; i <= _betweenNumber; i ++){
95
95
CGContextMoveToPoint (context, startX+lineCenterX*i, topY);
96
96
if (i%_betweenNumber == 0 ){
97
- NSString *num = [NSString stringWithFormat: @" %d " , i * _step + _minValue];
97
+ NSString *num = [NSString stringWithFormat: @" %d " , (int )(i * _step) + _minValue];
98
+ if ([num isEqualToString: @" 0" ]) {
99
+ num = @" 不设" ;
100
+ }
98
101
NSDictionary *attribute = @{NSFontAttributeName :TextRulerFont, NSForegroundColorAttributeName :[UIColor lightGrayColor ]};
99
102
CGFloat width = [num boundingRectWithSize: CGSizeMake (CGFLOAT_MAX, CGFLOAT_MAX) options: 0 attributes: attribute context: nil ].size .width ;
100
103
[num drawInRect: CGRectMake (startX+lineCenterX*i-width/2 , longLineY+10 , width, 16 ) withAttributes: attribute];
@@ -128,11 +131,18 @@ -(void)drawRect:(CGRect)rect{
128
131
CGContextSetLineCap (context, kCGLineCapButt );
129
132
130
133
CGContextMoveToPoint (context, rect.size .width , 0 );
131
- NSString *num = [NSString stringWithFormat: @" %d " ,_minValue];
134
+
135
+ NSString *num;
136
+ if (_minValue == 0 ) {
137
+ num = @" 不设" ;
138
+ } else {
139
+ num = [NSString stringWithFormat: @" %d " , _minValue];
140
+ }
141
+
132
142
NSDictionary *attribute = @{NSFontAttributeName :TextRulerFont,NSForegroundColorAttributeName :[UIColor lightGrayColor ]};
133
143
CGFloat width = [num boundingRectWithSize: CGSizeMake (CGFLOAT_MAX, CGFLOAT_MAX) options: 0 attributes: attribute context: nil ].size .width ;
134
144
[num drawInRect: CGRectMake (rect.size.width-width/2 , longLineY+10 , width, 16 ) withAttributes: attribute];
135
- CGContextAddLineToPoint (context,rect.size .width , longLineY);
145
+ CGContextAddLineToPoint (context, rect.size .width , longLineY);
136
146
CGContextStrokePath (context);// 开始绘制
137
147
}
138
148
@@ -179,7 +189,7 @@ @interface RCTScrollRuler()<UIScrollViewDelegate,UICollectionViewDelegate,UIColl
179
189
@property (nonatomic , assign )int stepNum;// 分多少个区
180
190
@property (nonatomic , assign )int minValue;// 游标的最小值
181
191
@property (nonatomic , assign )int maxValue;// 游标的最大值
182
- @property (nonatomic , assign )int step;// 间隔值,每两条相隔多少值
192
+ @property (nonatomic , assign )float step;// 间隔值,每两条相隔多少值
183
193
@property (nonatomic , assign )NSInteger betweenNum;
184
194
@property (nonatomic , strong )NSString *unit;// 单位
185
195
@property (nonatomic ,assign )int defaultValue;
@@ -223,7 +233,7 @@ - (void)setMaxValue:(int)maxValue {
223
233
self.unitLab .text = _unit;
224
234
}
225
235
226
- - (void )setStep : (int )step {
236
+ - (void )setStep : (float )step {
227
237
NSLog (@" 设置步长" );
228
238
[[self subviews ]makeObjectsPerformSelector:@selector (removeFromSuperview )];
229
239
_step = step;
@@ -244,7 +254,7 @@ - (void)setStep:(int)step {
244
254
- (void )setDefaultValue : (int )defaultValue {
245
255
NSLog (@" 设置默认值" );
246
256
_defaultValue = defaultValue;
247
- if (_minValue != 0 && _maxValue != 0 ) {
257
+ if (_maxValue != 0 ) {
248
258
[self setRealValue: defaultValue];
249
259
[_collectionView setContentOffset: CGPointMake (((defaultValue-_minValue)/(float )_step)*RulerGap, 0 ) animated: YES ];
250
260
}
@@ -384,7 +394,14 @@ -(void)setRealValue:(int)realValue{
384
394
}
385
395
-(void )setRealValue : (float )realValue animated : (BOOL )animated {
386
396
_realValue = realValue;
387
- _valueLab.text = [NSString stringWithFormat: @" %d " ,_realValue*_step+_minValue];
397
+ int n = _realValue*_step+_minValue;
398
+ if (n == 0 ) {
399
+ _unitLab.hidden = YES ;
400
+ _valueLab.text = @" 不设" ;
401
+ } else {
402
+ _unitLab.hidden = NO ;
403
+ _valueLab.text = [NSString stringWithFormat: @" %d " , n];
404
+ }
388
405
[_collectionView setContentOffset: CGPointMake ((int )realValue*RulerGap, 0 ) animated: animated];
389
406
}
390
407
@@ -479,9 +496,13 @@ -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
479
496
if (totalValue >= _maxValue) {
480
497
_valueLab.text = [NSString stringWithFormat: @" %d " ,_maxValue];
481
498
}else if (totalValue <= _minValue){
482
- _valueLab.text = [NSString stringWithFormat: @" %d " ,_minValue];
499
+ if (_minValue == 0 ) {
500
+ _valueLab.text = @" 不设" ;
501
+ } else {
502
+ _valueLab.text = [NSString stringWithFormat: @" %d " ,_minValue];
503
+ }
483
504
}else {
484
- _valueLab.text = [NSString stringWithFormat: @" %d " ,value*_step +_minValue];
505
+ _valueLab.text = [NSString stringWithFormat: @" %d " ,( int )( value*_step) +_minValue];
485
506
}
486
507
}
487
508
}
0 commit comments