滑块值没有显示在我的 imageView 中?
我有一个滑块,想要在图像视图中显示其值,该视图位于其上方并与滑块的拇指接触。 我添加了 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);
}
I have a Slider and want to show its value in a Image view which goes above it in touch with the thumb of the slider.
I have added the ImageView and the label to show the value in a UIView and making it move as the slider thumb is moving. But for some reason its not displaying the value of the slider although i checked that we are receiving the value.
-(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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您设置了两次文本,但我猜这只是您调试问题的一部分,对吗?
这两行是多余的:
尝试在那里放置一个日志以进行测试,将上面的两行替换为:
我的猜测是 val 将在那里显示为 nil 。
You are setting the text twice, but I'm guessing that was just part of your debugging the problem right?
These two lines are redundant:
Try putting a log there for the sake of testing, replace those two above with:
My guess is that val will show as nil there.