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)
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?
– Paulo Rodrigues
Hello @Paulorodrigues I am included by the Builder interface, and the label is not part of the library.
– Filipe Amaral Neis
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?
– Paulo Rodrigues
It is more or less that, only that the white circle it is not selectable, the user can not slide the same.
– Filipe Amaral Neis
@Paulorodrigues some idea how I can do the same ?
– Filipe Amaral Neis