在UISlider的拇指图像上连续更改UILabel的值 [英] Continuously change value of UILabel on thumb image of UISlider

查看:173
本文介绍了在UISlider的拇指图像上连续更改UILabel的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UISlider (最小1,最大10)。我希望它的拇指放置在它上面的 UILabel ,在移动 UISlider 时不断更新和更改其文本的拇指。所以,我从 UISlider 中抓取了拇指图像,并向其添加了 UILabel ,但标签似乎覆盖了自己而没有删除移动拇指后的前一个值。

I have a UISlider(min 1 ,max 10). I want its thumb to have a UILabel placed on top of it that continuously updates and changes its text on moving the UISlider's thumb. So, I grabbed thumb image from the UISlider and added a UILabel to it but the label seems to overwrite itself without erasing the previous value once the thumb is moved.

- (IBAction)SnoozeSliderValueChanged:(id)sender {

    UIImageView *handleView = [_snoozeSlider.subviews lastObject];
    UILabel *label = [[UILabel alloc] initWithFrame:handleView.bounds];
    label.text = [NSString stringWithFormat:@"%0.0f", self.snoozeSlider.value];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    [handleView addSubview:label];

}

最初,

然后,当我开始拖动时,

Then, when i start dragging,

我希望标签擦除之前的值,并在移动拇指时显示当前值。感谢任何帮助。谢谢!

I want the label to erase the previous value and show current value as the thumb is moved. Any help is appreciated.Thanks!

推荐答案

您的代码会不断添加新的子视图。删除现有的子视图:

Your code continuously adds new subviews. Remove the existing subviews with:

[handleView.subviews makeObjectsPerformSelector:@(removeFromSuperview)];

或者,重复使用现有标签。也许使用标签和 viewWithTag:来查找现有标签和更新(或创建,如果没有找到)。重用比娱乐更有效。

Alternatively, reuse the existing label. Perhaps using a tag and viewWithTag: to find the existing label and update (or create if not found). Reuse is more efficient than recreation.

这篇关于在UISlider的拇指图像上连续更改UILabel的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆