swiftsliderconstantsnslayoutconstraintcgfloat

slider only increasing imageview size not decreasing size


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





    }

}

Solution

  • In viewDidLoad() you are defining the constraints for your "blue view" based on the slider having a value of 0.0 ... but you are giving your slider an initial value of 0.5.

    So when you first touch the slider, your blue view's size "jumps" to a 0.5 adjusted size.

    Add a call to hhh() at the end of viewDidLoad():

        slider.addTarget(self, action: #selector(hhh), for: .allEvents)
    
        // change blue view based on initial slider value of 0.5
        hhh()
    

    Now you'll see the blue view get smaller when you drag the slider to the left, and larger when you drag it to the right.