使用滑块来操纵受 swift 中乘数约束的锚点
Use slider to manipulate anchors constrained by a multiplier in swift
我想使用滑块使图片图像视图变小。现在它的最大尺寸。滑块应该能够使其变小。当我使用滑块时,图片确实减少了,但我不能增加它,我只能减少它。这是我想要做的事情的照片。
我的代码在所有代码中都没有使用故事板。
import UIKit
class ViewController: UIViewController {
var sd = UISlider()
var pic = UIImageView()
var start = [NSLayoutConstraint]()
var change = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[sd,pic].forEach{
[=11=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=11=])
[=11=].backgroundColor = .systemOrange
}
pic.topAnchor.constraint(equalTo: view.topAnchor, constant: 12).isActive = true
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
start = [
pic.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
pic.heightAnchor.constraint(equalTo: view.layoutMarginsGuide.heightAnchor, multiplier: 1/2),
sd.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
sd.topAnchor.constraint(equalTo: view.centerYAnchor, constant : +250),
sd.widthAnchor.constraint(equalToConstant: 150),
sd.heightAnchor.constraint(equalToConstant: 40)
]
NSLayoutConstraint.activate(start)
sd.addTarget(self, action: #selector(moveRight), for: .allTouchEvents)
}
@objc func moveRight(_ sender: UISlider) {
NSLayoutConstraint.deactivate(start)
let jessicaAlba = 300 + ( 300 * CGFloat(sd.value) )
print(CGFloat(sd.value))
pic.topAnchor.constraint(equalTo: view.topAnchor, constant: -jessicaAlba).isActive = true
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: jessicaAlba).isActive = true
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -jessicaAlba).isActive = true
start = [
pic.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
pic.heightAnchor.constraint(equalTo: view.layoutMarginsGuide.heightAnchor, multiplier: 1/2),
sd.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
sd.topAnchor.constraint(equalTo: view.centerYAnchor, constant : +250),
sd.widthAnchor.constraint(equalToConstant: 100),
sd.heightAnchor.constraint(equalToConstant: 40)
]
NSLayoutConstraint.activate (start)
}
}
为非常粗略的代码道歉,但这有效:
var myView = UIView()
var topConstraint: NSLayoutConstraint!
var bottomConstraint: NSLayoutConstraint!
var leadingConstraint: NSLayoutConstraint!
var trailingConstraint: NSLayoutConstraint!
var jessicaAlba:Float = 50
var slider = UISlider()
override func viewDidLoad() {
super.viewDidLoad()
myView.translatesAutoresizingMaskIntoConstraints = false
myView.backgroundColor = UIColor.orange
view.addSubview(myView)
slider.translatesAutoresizingMaskIntoConstraints = false
slider.minimumValue = 50
slider.maximumValue = 200
slider.setValue(jessicaAlba, animated: false)
slider.addTarget(self, action: #selector(changeSize), for: .valueChanged)
view.addSubview(slider)
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
slider.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
slider.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10).isActive = true
topConstraint = myView.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
topConstraint.isActive = true
bottomConstraint = myView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: CGFloat(-jessicaAlba))
bottomConstraint.isActive = true
leadingConstraint = myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
leadingConstraint.isActive = true
trailingConstraint = myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
trailingConstraint.isActive = true
}
@objc func changeSize() {
jessicaAlba = slider.value
topConstraint.constant = CGFloat(jessicaAlba)
bottomConstraint.constant = CGFloat(-jessicaAlba)
leadingConstraint.constant = CGFloat(jessicaAlba)
trailingConstraint.constant = CGFloat(-jessicaAlba)
}
最主要的是:
- 声明您要更改的具体约束条件
- 在单独的行中设置
isActive = true
(我知道,愚蠢但是编译器不识别 NSLayoutConstraint 类型并且如果你在一行中这样做将不会构建
- 根据需要更改约束
我想使用滑块使图片图像视图变小。现在它的最大尺寸。滑块应该能够使其变小。当我使用滑块时,图片确实减少了,但我不能增加它,我只能减少它。这是我想要做的事情的照片。
我的代码在所有代码中都没有使用故事板。
import UIKit
class ViewController: UIViewController {
var sd = UISlider()
var pic = UIImageView()
var start = [NSLayoutConstraint]()
var change = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[sd,pic].forEach{
[=11=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=11=])
[=11=].backgroundColor = .systemOrange
}
pic.topAnchor.constraint(equalTo: view.topAnchor, constant: 12).isActive = true
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
start = [
pic.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
pic.heightAnchor.constraint(equalTo: view.layoutMarginsGuide.heightAnchor, multiplier: 1/2),
sd.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
sd.topAnchor.constraint(equalTo: view.centerYAnchor, constant : +250),
sd.widthAnchor.constraint(equalToConstant: 150),
sd.heightAnchor.constraint(equalToConstant: 40)
]
NSLayoutConstraint.activate(start)
sd.addTarget(self, action: #selector(moveRight), for: .allTouchEvents)
}
@objc func moveRight(_ sender: UISlider) {
NSLayoutConstraint.deactivate(start)
let jessicaAlba = 300 + ( 300 * CGFloat(sd.value) )
print(CGFloat(sd.value))
pic.topAnchor.constraint(equalTo: view.topAnchor, constant: -jessicaAlba).isActive = true
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: jessicaAlba).isActive = true
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -jessicaAlba).isActive = true
start = [
pic.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
pic.heightAnchor.constraint(equalTo: view.layoutMarginsGuide.heightAnchor, multiplier: 1/2),
sd.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
sd.topAnchor.constraint(equalTo: view.centerYAnchor, constant : +250),
sd.widthAnchor.constraint(equalToConstant: 100),
sd.heightAnchor.constraint(equalToConstant: 40)
]
NSLayoutConstraint.activate (start)
}
}
为非常粗略的代码道歉,但这有效:
var myView = UIView()
var topConstraint: NSLayoutConstraint!
var bottomConstraint: NSLayoutConstraint!
var leadingConstraint: NSLayoutConstraint!
var trailingConstraint: NSLayoutConstraint!
var jessicaAlba:Float = 50
var slider = UISlider()
override func viewDidLoad() {
super.viewDidLoad()
myView.translatesAutoresizingMaskIntoConstraints = false
myView.backgroundColor = UIColor.orange
view.addSubview(myView)
slider.translatesAutoresizingMaskIntoConstraints = false
slider.minimumValue = 50
slider.maximumValue = 200
slider.setValue(jessicaAlba, animated: false)
slider.addTarget(self, action: #selector(changeSize), for: .valueChanged)
view.addSubview(slider)
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
slider.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
slider.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10).isActive = true
topConstraint = myView.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
topConstraint.isActive = true
bottomConstraint = myView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: CGFloat(-jessicaAlba))
bottomConstraint.isActive = true
leadingConstraint = myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
leadingConstraint.isActive = true
trailingConstraint = myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
trailingConstraint.isActive = true
}
@objc func changeSize() {
jessicaAlba = slider.value
topConstraint.constant = CGFloat(jessicaAlba)
bottomConstraint.constant = CGFloat(-jessicaAlba)
leadingConstraint.constant = CGFloat(jessicaAlba)
trailingConstraint.constant = CGFloat(-jessicaAlba)
}
最主要的是:
- 声明您要更改的具体约束条件
- 在单独的行中设置
isActive = true
(我知道,愚蠢但是编译器不识别 NSLayoutConstraint 类型并且如果你在一行中这样做将不会构建 - 根据需要更改约束