【问题标题】:Continuously change value of UILabel on thumb image of UISlider在 UISlider 的缩略图上不断更改 UILabel 的值
【发布时间】:2014-03-13 08:30:24
【问题描述】:

我有一个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];

}

最初,

然后,当我开始拖动时,

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

【问题讨论】:

  • 最初,您是指在您第一次实际触摸滑块之后?因为最初,意思是在创建UISlider 之后,它没有子视图,所以这个方法不适用于在第一次实际触摸滑块之前创建标签...

标签: ios objective-c iphone uislider addsubview


【解决方案1】:
    - (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];

    }

【讨论】:

  • 检查更新的代码和描述。这将提高您的性能,而不是 removefromsuperview 方法
  • 清除!但是,一旦我转到其他视图并返回此视图,我注意到 UILabel 不会再次初始化,并且像往常一样,标签会尝试自我更新。奇怪的是,当我记录标签时,我注意到标签值发生了应有的变化,但正在运行的应用程序中的标签不再变化
  • 尝试@"%.0f" 而不是@"%0.0f"
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    这是因为,在每个滑块值更改事件中,您都在创建新的 UILabel 实例并将其添加到视图中。您可以只创建一次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];
    

    【讨论】:

      【解决方案4】:

      为了类似的目的,我创建了一个 Swift 类。它的名字是MBSliderView
      它可以在故事板和代码中使用。

      它有 2 种显示模式用于显示当前滑块值。
      在正常状态下,它在拇指中显示如下所示的值:

      当拇指按下时,它会显示如下的当前值:

      从代码中可以这样使用:

      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)
          }
      }
      

      为了接收滑块值,您必须实现MBSliderDelegate。像这样的:

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

      Here 是完整的示例代码。

      我知道现在回答很晚,但希望这对您或其他人有所帮助

      【讨论】:

      • 当时没有 swift :/
      • @motox 很快!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      相关资源
      最近更新 更多