I want my swift code below to keep the slider value of the width constraint when the sliders value is used for something else. You can see what is going on the gif below. When the value of the slider is used for something else the constraint changes. The constraints should not change when the value of the slider is considered for something else.
import UIKit
class ViewController: UIViewController {
var frontBox = UIButton()
var backBox = UIButton()
var selectorB = UIButton()
var slider = UISlider()
var slidermultipliera: CGFloat = 0.6
var slidermultiplierb: CGFloat = 0.6
var selctorValue = 0
// constraint we will modify when slider is changed
var backBoxWidth: NSLayoutConstraint!
// constraints we will modify when backBox is dragged
var backBoxCenterY: NSLayoutConstraint!
var backBoxLeading: NSLayoutConstraint!
var FrontBoxWidth: NSLayoutConstraint!
// constraints we will modify when backBox is dragged
var FrontBoxCenterY: NSLayoutConstraint!
var FrontBoxLeading: NSLayoutConstraint!
var tim = 50.0
override func viewDidLoad() {
super.viewDidLoad()
[backBox,selectorB,frontBox,slider].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
$0.backgroundColor = UIColor(
red: .random(in: 0.0...1),
green: .random(in: 0.9...1),
blue: .random(in: 0.7...1),
alpha: 1
)
}
selectorB.setTitle("right", for: .normal)
NSLayoutConstraint.activate([
selectorB.bottomAnchor.constraint(equalTo: view.bottomAnchor),
selectorB.leadingAnchor.constraint(equalTo: view.leadingAnchor),
selectorB.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1),
selectorB.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1),
slider.bottomAnchor.constraint(equalTo: selectorB.topAnchor),
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor),
slider.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1),
slider.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1),
])
// backBox Width constraint
backBoxWidth = backBox.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.2)
// backBox CenterY constraint
backBoxCenterY = backBox.centerYAnchor.constraint(equalTo: view.centerYAnchor)
// backBox Leading constraint
backBoxLeading = backBox.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: CGFloat(200))
// backBox Width constraint
FrontBoxWidth = frontBox.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.2)
// backBox CenterY constraint
FrontBoxCenterY = frontBox.centerYAnchor.constraint(equalTo: view.centerYAnchor)
// backBox Leading constraint
FrontBoxLeading = frontBox.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: CGFloat(tim))
slider.setValue(Float(0.5), animated: false)
NSLayoutConstraint.activate([
// backBox Height is constant
backBox.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.5),
backBoxWidth,
backBoxLeading,
backBoxCenterY,
frontBox.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.3),
FrontBoxWidth,
FrontBoxCenterY,
FrontBoxLeading,
])
selectorB.addTarget(self, action: #selector(press), for: .touchDown)
slider.addTarget(self, action: #selector(increase), for: .valueChanged)
}
@objc func press(){
selctorValue = selctorValue == 0 ? 1 : 0
if selctorValue == 1{
backBoxWidth.isActive = false
selectorB.setTitle("left", for: .normal)
}
else {
FrontBoxWidth.isActive = false
backBoxWidth.isActive = true
selectorB.setTitle("right", for: .normal)
}
}
@objc func increase() {
if selctorValue == 1{
slidermultipliera = CGFloat(slider.value)
// update backBox Width constraint
FrontBoxWidth.isActive = false
FrontBoxWidth = frontBox.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: slidermultipliera)
FrontBoxWidth.isActive = true
}
else {
slidermultiplierb = CGFloat(slider.value)
// update backBox Width constraint
backBoxWidth.isActive = false
backBoxWidth = backBox.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: slidermultiplierb)
backBoxWidth.isActive = true
}
}
}
I've never messed with Swift, but it seems like when you click one of the boxes, you should be able to set the slider value based on the width of the box you just clicked before making that box active.
I'm guessing it would happen in your @objc func press()
function. Try making both boxes inactive, then calling slider.setValue()
and then activating the box which was just clicked.
Sorry I couldn't give you working code, but hopefully that helps.