swiftslidernslayoutconstraintboundscgfloat

use slider to to change width and height of NSLAYOUT constraint imageview


My swift's code goal is to use a slider to increase decrease the size of a imageview. The imageview pic should be able to increase decrease size depending on what the value of slide slider is. When the func is called the origanal constraints must be de activated so the width and height can be changed. I heard that it is possible to use animation blocks.

import UIKit

class ViewController: UIViewController {

    var pic = UIImageView()
    var slide = UISlider()
    var currentView: UIView?
    var oldCons = [NSLayoutConstraint]()

    override func viewDidLoad() {
        super.viewDidLoad()
        slide.backgroundColor = .systemPink

        pic.backgroundColor = .systemGreen


        [pic,slide].forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)



        }
        NSLayoutConstraint.activate([

            slide.heightAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.25),
            slide.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.25),
            slide.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :0),

            slide.topAnchor.constraint(equalTo: pic.bottomAnchor, constant :0),

        ])
        oldCons = [

            pic.topAnchor.constraint(equalTo: view.topAnchor, constant :0),
            pic.heightAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.25),
            pic.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.25),
            pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :0),



        ]
        NSLayoutConstraint.activate(oldCons)



        slide.addTarget(self, action: #selector(change), for: .allTouchEvents)

    }


    @objc func change(){
        NSLayoutConstraint.deactivate(oldCons)
        pic.bounds.size.width = CGFloat(slide.value)
        pic.bounds.size.height = CGFloat(slide.value)

    }


}

Solution

  • You don't need to deactivate your constraints. You just need to save them and update the constants as the slider slides.

    class ViewController: UIViewController {
    
        var pic = UIImageView()
        var slide = UISlider()
        var currentView: UIView?
    
        var picWidth: NSLayoutConstraint!
        var picHeight: NSLayoutConstraint!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // ...
    
            picWidth = pic.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.25)
            picHeight = pic.heightAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.25)
            NSLayoutConstraint.activate([
                slide.heightAnchor.constraint(equalTo:  view.heightAnchor ,multiplier:  0.25),
                slide.widthAnchor.constraint(equalTo:  view.widthAnchor ,multiplier:  0.25),
                slide.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :0),
                slide.topAnchor.constraint(equalTo: pic.bottomAnchor, constant :0),
    
                pic.topAnchor.constraint(equalTo: view.topAnchor, constant :0),
                picWidth,
                picHeight,
                pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :0),
            ])
    
            // ...
        }
    
        @objc func change(){
            picWidth.constant = CGFloat(slide.value) * view.frame.size.width * 0.25
            picHeight.constant = CGFloat(slide.value) * view.frame.size.height * 0.25
        }
    
    }