简体   繁体   English

在UISlider的拇指图像上连续更改UILabel的值

[英]Continuously change value of UILabel on thumb image of UISlider

I have a UISlider (min 1 ,max 10). 我有一个UISlider (最小1,最大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. 我希望它的拇指放置在它上面的UILabel ,在移动UISlider的拇指时不断更新和更改其文本。 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. 所以,我抓住了UISlider拇指图像并添加了一个UILabel ,但是一旦拇指移动,标签似乎会覆盖自己而不会删除之前的值。

- (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];

}

Initially, 原来,

在此输入图像描述

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! 任何帮助表示赞赏。谢谢!

    - (IBAction)SnoozeSliderValueChanged:(id)sender {

        //Get the Image View
        UIImageView *handleView = [_snoozeSlider.subviews lastObject];

        // Get the Slider value label
        UILabel *label = (UILabel*)[handleView viewWithTag:1000];

        // If the slider label not exist then create it and add it to the Handleview. So handle view will have only one slider value label, so no more memory issues & not needed to remove from superview.
        // Creation of object is Pain to iOS. So simply reuse it by creating only once.
        // Note that tag setting below, which will helpful to find out that view presents in later case
        if (label==nil) {

            label = [[UILabel alloc] initWithFrame:handleView.bounds];

            label.tag = 1000;

            label.backgroundColor = [UIColor clearColor];

            label.textAlignment = NSTextAlignmentCenter;

            [handleView addSubview:label];


        }

        // Update the slider value
        label.text = [NSString stringWithFormat:@"%0.0f", self.snoozeSlider.value];

    }

Your code continuously adds new subviews. 您的代码会不断添加新的子视图。 Remove the existing subviews with: 删除现有的子视图:

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

Alternatively, reuse the existing label. 或者,重用现有标签。 Perhaps using a tag and viewWithTag: to find the existing label and update (or create if not found). 也许使用标签和viewWithTag:来查找现有标签并更新(或创建,如果没有找到)。 Reuse is more efficient than recreation. 重用比娱乐更有效。

This is because, on each slider value changed event, you are creating new UILabel instance and adding this to view. 这是因为,在每个滑块值更改事件上,您正在创建新的UILabel实例并将其添加到视图中。 You can just create the UILabel once and on subsequent calls, just update the label frame and label text like below: 您只需创建UILabel一次,然后在后续调用中,只需更新标签框架和标签文本,如下所示:

UIImageView *handleView = [_snoozeSlider.subviews lastObject];
UILabel *label = (UILabel*)[handleView viewWithTag:10];

if (!label) { //Create new instance

    label = [[UILabel alloc] init];       
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.tag = 10; //Assign a tag so that you can get the instance from parentview next time
    [handleView addSubview:label];
}
[label setFrame:handleView.bounds]; 
label.text = [NSString stringWithFormat:@"%0.0f", self.snoozeSlider.value];

I've created a Swift Class for similar purpose. 我为类似的目的创建了一个Swift类。 Its name is MBSliderView 它的名字是MBSliderView
It can be used both in storyboard and from code. 它既可以在故事板中使用,也可以在代码中使用。

It has 2 display modes for displaying current slider value. 它有2种显示模式,用于显示当前滑块值。
In normal state, it shows value inside thumb like this: 在正常状态下,它在拇指内显示如下值: 在此输入图像描述

And when thumb is pressed, it displays the current value like this: 当按下拇指时,它会显示当前值,如下所示: 在此输入图像描述

From code it can be used like this: 从代码中可以像这样使用:

class ViewController: UIViewController {

    var sliderFromCode = MBSliderView()

    override func viewDidLoad() {
        super.viewDidLoad()

        // configure sliderFromCode
        sliderFromCode.frame = CGRect(x: 10, y: 300, width: self.view.frame.size.width - 20, height: 100)
        sliderFromCode.minValue = 0
        sliderFromCode.maxValue = 10
        sliderFromCode.currentValue = 3
        sliderFromCode.step = 1
        sliderFromCode.ignoreDecimals = true   // default value
        sliderFromCode.animateLabel = true      // default value
        sliderFromCode.delegate = self

        self.view.addSubview(sliderFromCode)
    }
}

and in order to receive the slider value, you must implement the MBSliderDelegate . 并且为了接收滑块值,您必须实现MBSliderDelegate Something like this: 像这样的东西:

extension ViewController: MBSliderDelegate {
    func sliderView(_ sliderView: MBSliderView, valueDidChange value: Float) {
        print("sliderFromCode: \(value)")
    }
}

Here is the complete sample code. 是完整的示例代码。

I know its very to late to answer, but hope this helps you or someone else 我知道它很晚才回答,但希望这可以帮助你或其他人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM