Skip to content

Commit a990cb9

Browse files
committed
fixed bug
1 parent 4ffe535 commit a990cb9

File tree

6 files changed

+70
-23
lines changed

6 files changed

+70
-23
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
# react-native-scroll-ruler
22
ReactNative版滑动刻度尺,兼容Android和iOS。
3+
4+
5+
6+
minValue 尺子显示的最小值
7+
maxValue 尺子显示的最大值
8+
defaultValue 尺子默认值
9+
step 两个大刻度之间的数值间隔
10+
num 两个小刻度之间的数值间隔
11+
unit 单位
12+
13+
onSelect 选中值后的回调方法

android/src/main/java/com/shenhuniurou/scrollruler/RNScrollRuler.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,15 @@ public void onAnimationEnd(Animator animation) {
519519
} else {
520520
//绘制刻度,绘制刻度数字
521521
canvas.drawLine(0, 0, 0, midScaleHeight, midScalePaint);
522-
scaleNumPaint.getTextBounds(num1 / scaleGap + minScale + "", 0, (num1 / scaleGap + minScale + "").length(), scaleNumRect);
523-
canvas.drawText((num1 / scaleCount + minScale) * scaleLimit + "", -scaleNumRect.width() / 2, lagScaleHeight +
524-
(rulerHeight - lagScaleHeight) / 2 + scaleNumRect.height(), scaleNumPaint);
522+
if (num1 == 0 && minScale == 0) {
523+
scaleNumPaint.getTextBounds("不设", 0, "不设".length(), scaleNumRect);
524+
canvas.drawText("不设", -scaleNumRect.width() / 2, lagScaleHeight +
525+
(rulerHeight - lagScaleHeight) / 2 + scaleNumRect.height(), scaleNumPaint);
526+
} else {
527+
scaleNumPaint.getTextBounds(num1 / scaleGap + minScale + "", 0, (num1 / scaleGap + minScale + "").length(), scaleNumRect);
528+
canvas.drawText((num1 / scaleCount + minScale) * scaleLimit + "", -scaleNumRect.width() / 2, lagScaleHeight +
529+
(rulerHeight - lagScaleHeight) / 2 + scaleNumRect.height(), scaleNumPaint);
530+
}
525531

526532
}
527533

@@ -550,11 +556,20 @@ private void drawResultText(Canvas canvas, String resultText) {
550556
return;
551557
}
552558
canvas.translate(0, -resultNumRect.height() - rulerToResultgap / 2); //移动画布到正确的位置来绘制结果值
553-
resultNumPaint.getTextBounds(resultText, 0, resultText.length(), resultNumRect);
554-
canvas.drawText(resultText, width / 2 - resultNumRect.width() / 2, resultNumRect.height(), //绘制当前刻度结果值
555-
resultNumPaint);
556-
resultNumRight = width / 2 + resultNumRect.width() / 2 + 5;
557-
canvas.drawText(unit, resultNumRight, kgRect.height() + 8, kgPaint); //在当前刻度结果值的又面10px的位置绘制单位
559+
if (resultText.equals("0")) {
560+
resultText = "不设";
561+
resultNumPaint.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
562+
resultNumPaint.getTextBounds(resultText, 0, resultText.length(), resultNumRect);
563+
canvas.drawText(resultText, width / 2 - resultNumRect.width() / 2, resultNumRect.height(), //绘制当前刻度结果值
564+
resultNumPaint);
565+
} else {
566+
resultNumPaint.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
567+
resultNumPaint.getTextBounds(resultText, 0, resultText.length(), resultNumRect);
568+
canvas.drawText(resultText, width / 2 - resultNumRect.width() / 2, resultNumRect.height(), //绘制当前刻度结果值
569+
resultNumPaint);
570+
resultNumRight = width / 2 + resultNumRect.width() / 2 + 5;
571+
canvas.drawText(unit, resultNumRight, kgRect.height() + 8, kgPaint); //在当前刻度结果值的又面10px的位置绘制单位
572+
}
558573
}
559574

560575
private void drawBg(Canvas canvas) {

android/src/main/java/com/shenhuniurou/scrollruler/RNScrollRulerManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void setUnit(RNScrollRuler ruler, @Nullable String unit) {
6767
}
6868

6969
@ReactProp(name = "step")
70-
public void setStep(RNScrollRuler ruler, @Nullable int step) {
71-
ruler.setScaleLimit(step * 10);
70+
public void setStep(RNScrollRuler ruler, @Nullable float step) {
71+
ruler.setScaleLimit((int)(step * 10));
7272
}
7373

7474
@ReactProp(name = "num")

ios/RCTScrollRuler/RCTScrollRuler.m

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ @interface DYRulerView : UIView
7474
@property (nonatomic,assign)NSInteger betweenNumber;
7575
@property (nonatomic,assign)int minValue;
7676
@property (nonatomic,assign)int maxValue;
77-
@property (nonatomic,assign)int step;
77+
@property (nonatomic,assign)float step;
7878

7979
@end
8080
@implementation DYRulerView
@@ -94,7 +94,10 @@ -(void)drawRect:(CGRect)rect{
9494
for (int i = 0; i <= _betweenNumber; i ++){
9595
CGContextMoveToPoint(context, startX+lineCenterX*i, topY);
9696
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+
}
98101
NSDictionary *attribute = @{NSFontAttributeName:TextRulerFont, NSForegroundColorAttributeName:[UIColor lightGrayColor]};
99102
CGFloat width = [num boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:0 attributes:attribute context:nil].size.width;
100103
[num drawInRect:CGRectMake(startX+lineCenterX*i-width/2, longLineY+10, width, 16) withAttributes:attribute];
@@ -128,11 +131,18 @@ -(void)drawRect:(CGRect)rect{
128131
CGContextSetLineCap(context, kCGLineCapButt);
129132

130133
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+
132142
NSDictionary *attribute = @{NSFontAttributeName:TextRulerFont,NSForegroundColorAttributeName:[UIColor lightGrayColor]};
133143
CGFloat width = [num boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:0 attributes:attribute context:nil].size.width;
134144
[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);
136146
CGContextStrokePath(context);//开始绘制
137147
}
138148

@@ -179,7 +189,7 @@ @interface RCTScrollRuler()<UIScrollViewDelegate,UICollectionViewDelegate,UIColl
179189
@property(nonatomic, assign)int stepNum;//分多少个区
180190
@property(nonatomic, assign)int minValue;//游标的最小值
181191
@property(nonatomic, assign)int maxValue;//游标的最大值
182-
@property(nonatomic, assign)int step;//间隔值,每两条相隔多少值
192+
@property(nonatomic, assign)float step;//间隔值,每两条相隔多少值
183193
@property(nonatomic, assign)NSInteger betweenNum;
184194
@property(nonatomic, strong)NSString *unit;//单位
185195
@property (nonatomic,assign)int defaultValue;
@@ -223,7 +233,7 @@ - (void)setMaxValue:(int)maxValue {
223233
self.unitLab.text = _unit;
224234
}
225235

226-
- (void)setStep:(int)step {
236+
- (void)setStep:(float)step {
227237
NSLog(@"设置步长");
228238
[[self subviews]makeObjectsPerformSelector:@selector(removeFromSuperview)];
229239
_step = step;
@@ -244,7 +254,7 @@ - (void)setStep:(int)step {
244254
- (void)setDefaultValue:(int)defaultValue {
245255
NSLog(@"设置默认值");
246256
_defaultValue = defaultValue;
247-
if (_minValue != 0 && _maxValue != 0) {
257+
if (_maxValue != 0) {
248258
[self setRealValue:defaultValue];
249259
[_collectionView setContentOffset:CGPointMake(((defaultValue-_minValue)/(float)_step)*RulerGap, 0) animated:YES];
250260
}
@@ -384,7 +394,14 @@ -(void)setRealValue:(int)realValue{
384394
}
385395
-(void)setRealValue:(float)realValue animated:(BOOL)animated{
386396
_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+
}
388405
[_collectionView setContentOffset:CGPointMake((int)realValue*RulerGap, 0) animated:animated];
389406
}
390407

@@ -479,9 +496,13 @@ -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
479496
if (totalValue >= _maxValue) {
480497
_valueLab.text = [NSString stringWithFormat:@"%d",_maxValue];
481498
}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+
}
483504
}else{
484-
_valueLab.text = [NSString stringWithFormat:@"%d",value*_step +_minValue];
505+
_valueLab.text = [NSString stringWithFormat:@"%d",(int)(value*_step) +_minValue];
485506
}
486507
}
487508
}

ios/RCTScrollRuler/RCTScrollRulerManager.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ @implementation RCTScrollRulerManager
2929

3030
RCT_EXPORT_VIEW_PROPERTY(maxValue, int);
3131

32-
RCT_EXPORT_VIEW_PROPERTY(step, int);
32+
RCT_EXPORT_VIEW_PROPERTY(step, float);
3333

3434
RCT_EXPORT_VIEW_PROPERTY(defaultValue, int);
3535

@@ -43,7 +43,7 @@ - (UIView *)view
4343
{
4444

4545
CGFloat rullerHeight = [RCTScrollRuler rulerViewHeight];
46-
_noneZeroRullerView = [[RCTScrollRuler alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth-20, rullerHeight) theMinValue:0 theMaxValue:0 theStep:1 theNum:10 theUnit:@""];
46+
_noneZeroRullerView = [[RCTScrollRuler alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth-20, rullerHeight) theMinValue:0 theMaxValue:0 theStep:1.0 theNum:10 theUnit:@""];
4747
_noneZeroRullerView.bgColor = [UIColor whiteColor];
4848
_noneZeroRullerView.delegate = self;
4949
_noneZeroRullerView.scrollByHand = YES;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-scroll-ruler",
3-
"version": "1.1.7",
3+
"version": "1.2.1",
44
"description": "ReactNative版选择身高体重的横向刻度尺组件,兼容Android和iOS",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)