My swift code code below uses a slider to increase the size of the imageview. What I would like to do is when the slider moves from less than 0.5 the imageview gets smaller. If the value of the slider is equal to or greater than 0.5 the size of the imageveiw increases. Right now the slider value is set at 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($0)
$0.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
}
}