Troubles With Starting Value Using Uislider

Troubles with starting value using UISlider

Yes, it's Xcode issues. You can find the issues list from this link. (https://fahimfarook.medium.com/xcode-12-and-ios-14-developer-bugs-and-issues-ada35920a104).

Create an outlet of both slider and set value through coding.

@IBOutlet weak var firstSlider: UISlider!
@IBOutlet weak var secondSlider: UISlider!

override func viewDidLoad() {
super.viewDidLoad()

firstSlider.minimumValue = 0
firstSlider.maximumValue = 3
firstSlider.value = 1.5

secondSlider.minimumValue = 0
secondSlider.maximumValue = 200
secondSlider.value = 100
}

UISlider ignores starting value

Create a IBOutlet like you did for the label and name it durationSlider. Then set the initial values in the viewDidLoad function:

let duration: Float = 5

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
durationSlider.minimumValue = 0
durationSlider.maximumValue = duration
durationSlider.value = 3 // set your startValue here

updateSliderLabel()
}

@IBAction func durationSliderChanged(_ sender: Any) {
print("currentValue: \(durationSlider.value)")
updateSliderLabel()
}

func updateSliderLabel() {
durationLabel.text = String(round(durationSlider.value)) + "s"
}

Setting UISlider value on app start

Put your code ([mySlider setValue:0]) in viewWillAppear if you don't want to see the slider value change, in viewDidAppear if you want to see it change.

viewDidLoad is only called when you create the view, it's not called when you display it after it has been loaded (for example, exiting the app and comming back, comming back from a push/pop in the navigation of a UINavigationController, etc.).

Note you can use this method : [mySlider setValue:0 Animated:YES]; if you want to see the animation.

UISlider how to set the initial value

Setting slider.value is the correct way if your linkage is correct (you have made the connections in Interface Builder). If you have created a UISlider in code, all you have to do is set the value property. If that is not working then be sure firstly that the object is "live" (correctly allocated, not released, not out of scope etc.) and secondly that the functions in which you set slider.value are actually being called.

If you are using Interface Builder and are not sure of how to connect your slider as an IBOutlet, you should check out iTunes University - search for "CS193P" to find some excellent videos from Stanford University. The first couple will take you through making those connections. If using IB and you have not made the connection - nothing will happen.

Tap on UISlider to Set the Value

Looks like you need to actually initialize the tap gesture recognizer in your viewDidLoad() per the code example above. There's a comment there, but I don't see the recognizer being created anywhere.

Swift 2:

class ViewController: UIViewController {

var slider: UISlider!

override func viewDidLoad() {
super.viewDidLoad()

// Setup the slider

// Add a gesture recognizer to the slider
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "sliderTapped:")
self.slider.addGestureRecognizer(tapGestureRecognizer)
}

func sliderTapped(gestureRecognizer: UIGestureRecognizer) {
// print("A")

let pointTapped: CGPoint = gestureRecognizer.locationInView(self.view)

let positionOfSlider: CGPoint = slider.frame.origin
let widthOfSlider: CGFloat = slider.frame.size.width
let newValue = ((pointTapped.x - positionOfSlider.x) * CGFloat(slider.maximumValue) / widthOfSlider)

slider.setValue(Float(newValue), animated: true)
}
}

Swift 3:

class ViewController: UIViewController {

var slider: UISlider!

override func viewDidLoad() {
super.viewDidLoad()

// Setup the slider

// Add a gesture recognizer to the slider
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sliderTapped(gestureRecognizer:)))
self.slider.addGestureRecognizer(tapGestureRecognizer)
}

func sliderTapped(gestureRecognizer: UIGestureRecognizer) {
// print("A")

let pointTapped: CGPoint = gestureRecognizer.location(in: self.view)

let positionOfSlider: CGPoint = slider.frame.origin
let widthOfSlider: CGFloat = slider.frame.size.width
let newValue = ((pointTapped.x - positionOfSlider.x) * CGFloat(slider.maximumValue) / widthOfSlider)

slider.setValue(Float(newValue), animated: true)
}
}

Programmed UISlider value not changing

First you need to give that slider positioning constraints like top , leading and width as you set $0.translatesAutoresizingMaskIntoConstraints = false which invalidates setting the frame otherwise comment it and leave the frame line

Second default values for the slider's minimumValue , maximumValue are 0,1 respectively so you need to set them according to what you need


override func viewDidLoad() {
super.viewDidLoad()

self.view.addSubview(move)
move.minimumValue = 10
move.maximumValue = 50
move.frame = CGRect(x: view.center.x-115, y: view.center.y+200, width: 200, height: 30)
move.addTarget(self, action: #selector(moveRight), for: .valueChanged)
}

@objc func moveRight(_ sender: UISlider) {
let currentValue = Int(sender.value)

print("ben:",currentValue)
}

How set the default value for my slider?

You can set the lowest potential value via the minimumValue method, the highest via the maximumValue method and the current value via the value method, all of which expect the value to be expressed as a float between 0.0 and 1.0.

See the UISlider class reference docs for more information.



Related Topics



Leave a reply



Submit