滑块仅增加 imageview 大小而不减小大小
slider only increasing imageview size not decreasing size
我的 swift 下面的代码使用滑块来增加图像视图的大小。我想做的是当滑块从小于 0.5 移动时,图像视图变小。如果滑块的值等于或大于 0.5,则 imageveiw 的大小会增加。现在滑块值设置为 0.5
import UIKit
class ViewController: UIViewController {
var image1Width2: NSLayoutConstraint!
var image1Height2: NSLayoutConstraint!
var slider = UISlider()
var blueMove = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
slider.value = 0.5
blueMove.backgroundColor = .blue
blueMove.isUserInteractionEnabled = true
blueMove.backgroundColor = .blue
[blueMove,slider].forEach {
view.addSubview([=10=])
[=10=].translatesAutoresizingMaskIntoConstraints = false
}
//image12
image1Width2 = blueMove.widthAnchor.constraint(equalTo: view.widthAnchor ,multiplier: 0.25)
image1Height2 = blueMove.heightAnchor.constraint(equalTo: view.heightAnchor ,multiplier: 0.20)
let percent1 = self.view.frame.height * 0.1
let percent2 = self.view.frame.width * 0.2
NSLayoutConstraint.activate([
blueMove.topAnchor.constraint(equalTo: view.topAnchor, constant : percent1),
image1Width2,
image1Height2,
blueMove.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :percent2)
])
slider.addTarget(self, action: #selector(hhh), for: .allEvents)
}
override func viewDidLayoutSubviews() {
NSLayoutConstraint.activate ([
slider.bottomAnchor.constraint(equalTo: view.bottomAnchor),
slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.20, constant: 0),
slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.20, constant: 0),
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
])
}
@objc func hhh() {
image1Width2.constant = CGFloat(slider.value) * view.frame.size.width * 0.25
image1Height2.constant = CGFloat(slider.value) * view.frame.size.height * 0.25
}
}
在 viewDidLoad()
中,您正在根据值为 0.0
的滑块定义 "blue view" 的约束...但是您为滑块提供的初始值为 0.5
。
因此,当您第一次触摸滑块时,您的蓝色视图的大小 "jumps" 调整为 0.5
大小。
在viewDidLoad()
末尾添加对hhh()
的调用:
slider.addTarget(self, action: #selector(hhh), for: .allEvents)
// change blue view based on initial slider value of 0.5
hhh()
现在您会看到蓝色视图在向左拖动滑块时变小,在向右拖动时变大。
我的 swift 下面的代码使用滑块来增加图像视图的大小。我想做的是当滑块从小于 0.5 移动时,图像视图变小。如果滑块的值等于或大于 0.5,则 imageveiw 的大小会增加。现在滑块值设置为 0.5
import UIKit
class ViewController: UIViewController {
var image1Width2: NSLayoutConstraint!
var image1Height2: NSLayoutConstraint!
var slider = UISlider()
var blueMove = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
slider.value = 0.5
blueMove.backgroundColor = .blue
blueMove.isUserInteractionEnabled = true
blueMove.backgroundColor = .blue
[blueMove,slider].forEach {
view.addSubview([=10=])
[=10=].translatesAutoresizingMaskIntoConstraints = false
}
//image12
image1Width2 = blueMove.widthAnchor.constraint(equalTo: view.widthAnchor ,multiplier: 0.25)
image1Height2 = blueMove.heightAnchor.constraint(equalTo: view.heightAnchor ,multiplier: 0.20)
let percent1 = self.view.frame.height * 0.1
let percent2 = self.view.frame.width * 0.2
NSLayoutConstraint.activate([
blueMove.topAnchor.constraint(equalTo: view.topAnchor, constant : percent1),
image1Width2,
image1Height2,
blueMove.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :percent2)
])
slider.addTarget(self, action: #selector(hhh), for: .allEvents)
}
override func viewDidLayoutSubviews() {
NSLayoutConstraint.activate ([
slider.bottomAnchor.constraint(equalTo: view.bottomAnchor),
slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.20, constant: 0),
slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.20, constant: 0),
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
])
}
@objc func hhh() {
image1Width2.constant = CGFloat(slider.value) * view.frame.size.width * 0.25
image1Height2.constant = CGFloat(slider.value) * view.frame.size.height * 0.25
}
}
在 viewDidLoad()
中,您正在根据值为 0.0
的滑块定义 "blue view" 的约束...但是您为滑块提供的初始值为 0.5
。
因此,当您第一次触摸滑块时,您的蓝色视图的大小 "jumps" 调整为 0.5
大小。
在viewDidLoad()
末尾添加对hhh()
的调用:
slider.addTarget(self, action: #selector(hhh), for: .allEvents)
// change blue view based on initial slider value of 0.5
hhh()
现在您会看到蓝色视图在向左拖动滑块时变小,在向右拖动时变大。