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

mot*_*tox 4 iphone objective-c uislider addsubview ios

我有一个UISlider(最小1,最大10).我希望它的拇指UILabel放在它的顶部,在移动UISlider拇指时不断更新和更改其文本.因此,我从中抓取了拇指图像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];

}
Run Code Online (Sandbox Code Playgroud)

原来,

在此输入图像描述

然后,当我开始拖动时,

在此输入图像描述

我希望标签擦除先前的值并在移动拇指时显示当前值.任何帮助表示赞赏.谢谢!

Vij*_*com 7

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

    }
Run Code Online (Sandbox Code Playgroud)