Slider value is not showing up in my imageView?
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;
开发者_如何学PythonCGFloat 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);
}
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:
val.text = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
[val setText:[NSString stringWithFormat:@"%3.0f", [localSlider value]]];
Try putting a log there for the sake of testing, replace those two above with:
NSString *valString = [NSString stringWithFormat:@"%3.0f", [localSlider value]];
[val setText:valString];
NSLog(@"val: %@, valString: %@", val, valString);
My guess is that val will show as nil there.
精彩评论