滑块值没有显示在我的 imageView 中?

发布于 2024-11-02 23:33:36 字数 2799 浏览 4 评论 0原文

我有一个滑块,想要在图像视图中显示其值,该视图位于其上方并与滑块的拇指接触。 我添加了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2024-11-09 23:33:36

您设置了两次文本,但我猜这只是您调试问题的一部分,对吗?

这两行是多余的:

 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 将在那里显示为 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:

 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文