Label Position - Swift IOS

Asked

Viewed 208 times

0

Good evening folks. How do I leave the label that is informing the current weight positioned below the progress bar cursor ?

To make this progress bar I am using Gradientslider (https://www.cocoacontrols.com/controls/gradientslider)

inserir a descrição da imagem aqui

Thanks in advance for your attention.

  • Filipe, I understand that the progress bar you are using the mentioned library, but apparently, the label below is not part of it, is it? If I’m right, how are you inserting this label? For Builder interface or directly in the code? You can demonstrate how you did it?

  • Hello @Paulorodrigues I am included by the Builder interface, and the label is not part of the library.

  • Now that I have read your question, I am in doubt. You want the current weight to follow the white circle as the user slides with his finger?

  • It is more or less that, only that the white circle it is not selectable, the user can not slide the same.

  • @Paulorodrigues some idea how I can do the same ?

2 answers

0

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

@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 method sliderValueChanged: is called when the Slider value is changed.
  • The method thumbCenterOnSlider: gets the center of the "white ball" (called "Thumb")
  • The method updateHintForSlider: updates the position and text of the label which, 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)
  • Hello @Brunoserrano, your method works more in my case it unfortunately does not work, even so thank you very much for the help and attention.

0

I simulated a possible way to do this using Uislider and the thumbRectForBounds method. However, I have found that Gradientslider does not have a direct method to pick up the size and position of the Thumb. I suggest you take a look at the Gradientslider code year, at 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)"
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.