Label Position - Swift IOS

0

Good evening everyone. How do I leave the label that is reporting the current weight positioned under the progress bar cursor?

To make this progress bar I'm using GradientSlider ( link )

Thanks in advance for your attention.

    
asked by anonymous 05.10.2015 / 04:22

2 answers

0

I've simulated a possible way to do this using UISlider and the thumbRectForBounds method. However, I noticed that GradientSlider does not have a straightforward method for grabbing the size and position of the thumb. I suggest you take a look at the GradientSlider year code in the _thumbLayer property. You will have to expose this property to expose the size of the thumb, and then you can use the technique I describe in the code below.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var valueLabel: UILabel!

    private var displayValue : String {
        get {
            let formatter = NSNumberFormatter()
            formatter.minimumFractionDigits = 1
            return formatter.stringFromNumber(slider.value)!
        }
    }

    @IBAction func moveLabel() {
        let thumbBounds = slider.thumbRectForBounds(view.bounds, trackRect: slider.trackRectForBounds(view.bounds), value: slider.value)

        let offsetXPercentage = (slider.value - slider.minimumValue) / (slider.maximumValue - slider.minimumValue)
        valueLabel.frame.origin.x = thumbBounds.origin.x + slider.frame.origin.x - slider.frame.origin.x * CGFloat(offsetXPercentage)
        valueLabel.text = "\(displayValue)"
    }

}
    
07.10.2015 / 03:42
0

I've done something similar, but the Slider could be moved by the user. I did this:

@IBAction func sliderValueChanged(sender: UISlider) {
  [...]

  self.updateHintForSlider(sender)
}

func updateHintForSlider(sender: UISlider) {
  var frame = self.hintLabel.frame
  frame.origin.x = thumbCenterOnSlider(sender) - (frame.size.width / 2)
  self.hintLabel.frame = frame

  self.hintLabel.text = NSString(format: "%.0f", sender.value) as String
}

func thumbCenterOnSlider(slider: UISlider) -> CGFloat {
  let sliderRange = slider.frame.size.width - slider.currentThumbImage!.size.width
  let sliderOrigin = slider.frame.origin.x + (slider.currentThumbImage!.size.width / 2.0)

  let sliderValueToPixels = CGFloat((slider.value - slider.minimumValue) / (slider.maximumValue - slider.minimumValue)) * sliderRange + sliderOrigin

  return sliderValueToPixels
}
  • The sliderValueChanged: method is called when the Slider value changes.
  • Method thumbCenterOnSlider: gets the center of the "white ball" (called "thumb")
  • The updateHintForSlider: method updates the position and text of the label that, in your case, informs the current weight ( self.hintLabel )

PS:

The label starts like this:

var frame = self.slider.frame
frame.size.width = 50
frame.size.height = 50
frame.origin.x = thumbCenterOnSlider(self.slider) - (frame.size.width / 2)
frame.origin.y = 20

self.hintLabel = UILabel(frame: frame)
    
07.10.2015 / 23:48