我有一个Slider,想要在一个图像视图中显示其值,该视图在滑块的拇指上方与其相对。 我添加了ImageView和标签,以显示UIView中的值,并在滑块拇指移动时使其移动。但由于某种原因,它没有显示滑块的值,虽然我检查了我们正在接收值。
-(void)addSliderX:frame andminimumValue:(int)min andmaximumValue:(int)max andSliderValue:(int)value{
CGRect frame1 = CGRectFromString(frame);
statSlider = [[UISlider alloc]initWithFrame:frame1];
[statSlider setMinimumTrackImage:[[UIImage imageNamed:@"greenSlider.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:10.0] forState:UIControlStateNormal];
[statSlider setThumbImage:[UIImage imageNamed:@"sliderBar_greenThumb.png"] forState:UIControlStateNormal];
[statSlider setMinimumValue:min];
[statSlider setMaximumValue:max];
statSlider.continuous = YES;
positionX = value;
[statSlider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
[statSlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventTouchUpInside];
UIView * bubble = [[UIView alloc]init];
UIImageView * back = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sliderBar_handle_number.png"]];
UILabel * val = [[UILabel alloc]init];
bubble.tag = 1;
back.tag = 2;
val.tag = 3;
bubble.alpha = 0;
back.alpha = 0;
val.alpha = 0;
[self.view addSubview:statSlider];
[self.view addSubview:bubble];
[bubble addSubview:back];
[bubble addSubview:val];
}
-(void)valueChanged:(id)sender{
UISlider *localSlider = (UISlider *)sender;
greenSliderValue = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
dsp =[greenSliderValue floatValue];
UIView * bubble = (UIView*)[self.view viewWithTag:1];
UIImageView * back = (UIImageView*)[bubble viewWithTag:2];
UILabel * val = (UILabel*)[bubble viewWithTag:3];
val.text = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
[val setText:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
UIFont * font = YEAR_LABEL_FONT;
val.font = font;
val.textColor = DESC_Color_White;
bubble.alpha = 1;
back.alpha = 1;
val.alpha = 1;
CGFloat sliderMax = localSlider.maximumValue;
CGFloat sliderMin = localSlider.minimumValue;
CGFloat sliderMaxMinDiff = sliderMax - sliderMin;
CGFloat sliderValue = dsp;
CGFloat xCoord = 65.0-42.0;
CGFloat yCoord = (CGFloat)positionX;
CGFloat halfMax = (sliderMax+sliderMin)/2.0;
if (sliderValue > halfMax)
{
xCoord = xCoord-(sliderValue-halfMax);
}
else
{
xCoord = xCoord + (halfMax-sliderValue);
}
xCoord= xCoord + (600/sliderMaxMinDiff)*(sliderValue-sliderMin);
back.frame = CGRectMake(xCoord,yCoord,84,47);
}
答案 0 :(得分:0)
您正在设置文本两次,但我猜这只是您调试问题的一部分吗?
这两行是多余的:
val.text = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
[val setText:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
尝试在那里放一个日志以便进行测试,将上面的两个替换为:
NSString *valString = [NSString stringWithFormat:@"%3.0f", [localSlider value]];
[val setText:valString];
NSLog(@"val: %@, valString: %@", val, valString);
我的猜测是val会在那里显示为零。