使用滑块控制 nslayoutConstraint 的宽度和高度

Use slider to control width and height of a nslayoutConstraint

我的代码在swift,我想用一个slider(sd)来控制imageview pic的宽度和高度。我在下面尝试的操作不起作用。只要我移动滑块,sd 和 pic 就会消失。滑块移动时图片应逐渐改变其宽度和高度。我看到调试区域有百分比变化,但它在图片上不起作用。我的代码不使用故事板。

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{
        [=10=].translatesAutoresizingMaskIntoConstraints = false
        view.addSubview([=10=])

        [=10=].backgroundColor = .systemOrange
    }

    start = [
        pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
        pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -225),
        pic.widthAnchor.constraint(equalToConstant: 300),
        pic.heightAnchor.constraint(equalToConstant: 450),

        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)

    sd.addTarget(self, action: #selector(moveRight), for: .allEvents)





}


@objc func moveRight() {

        NSLayoutConstraint.deactivate(start)

    let jessicaAlba = CGFloat(sd.value)
    print(jessicaAlba)


    NSLayoutConstraint.activate ([

        pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
        pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -225),
        pic.widthAnchor.constraint(equalToConstant: jessicaAlba),
        pic.heightAnchor.constraint(equalToConstant: jessicaAlba),

        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(change)


}


}

你的代码有两个问题。

1 ) 您已将 widthAnchor 和 heightAnchor 约束设置为 CGFloat(sd.value) 的常量,范围在 0 和 1 之间。这就是为什么您的图片看起来消失了。但实际上,它的宽度和高度被设置为小于 1 的值。

2 ) 您尚未从 moveRight 函数中停用先前设置的约束。

尝试下面的示例代码段,

    @objc func moveRight() {
        NSLayoutConstraint.deactivate(start)

        let jessicaAlba = 300 + ( 300 * CGFloat(sd.value) )
        print(CGFloat(sd.value))

        start = [

            pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
            pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -225),
            pic.widthAnchor.constraint(equalToConstant: jessicaAlba),
            pic.heightAnchor.constraint(equalToConstant: jessicaAlba),

            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)
    }