当滑块值用于其他用途时,我希望下面的快速代码保持宽度约束的滑块值。你可以在下面的 gif 中看到发生了什么。当滑块的值用于其他用途时,约束会发生变化。当滑块的值被考虑为其他东西时,约束不应该改变。
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
}
}
}