当滑块用于其他任务时保持原始滑块值
keep original slider value when slider become used for another task
我希望下面的 swift 代码在滑块值用于其他用途时保留宽度约束的滑块值。您可以在下面的 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{
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].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
}
}
}
我从来没有弄乱过Swift,但看起来当你点击其中一个框时,你应该能够根据你刚刚点击的框的宽度来设置滑块值,然后再制作那个盒子激活了。
我猜它会在您的 @objc func press()
函数中发生。尝试使两个框都处于非活动状态,然后调用 slider.setValue()
然后激活刚刚单击的框。
抱歉,我无法为您提供工作代码,但希望对您有所帮助。
我希望下面的 swift 代码在滑块值用于其他用途时保留宽度约束的滑块值。您可以在下面的 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{
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].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
}
}
}
我从来没有弄乱过Swift,但看起来当你点击其中一个框时,你应该能够根据你刚刚点击的框的宽度来设置滑块值,然后再制作那个盒子激活了。
我猜它会在您的 @objc func press()
函数中发生。尝试使两个框都处于非活动状态,然后调用 slider.setValue()
然后激活刚刚单击的框。
抱歉,我无法为您提供工作代码,但希望对您有所帮助。