如何以编程方式设置控制图像视图宽度的滑块

Posted

技术标签:

【中文标题】如何以编程方式设置控制图像视图宽度的滑块【英文标题】:How to programmatically set a slider that controls a image views width 【发布时间】:2019-11-17 05:34:16 【问题描述】:

我下面的代码是用没有故事板的所有代码编写的。我想要做的是使用滑块移动来更改 imageView pic 的宽度。我在下面尝试过的不起作用。当滑块移动时,我希望图片的宽度随之移动。

import UIKit

class ViewController: UIViewController 

    var pic = UIImageView()
    var move = UISlider()

    var oldCons = [NSLayoutConstraint]()
    var box = 20
    override func viewDidAppear(_ animated: Bool) 
        super.viewDidAppear(animated)
        pic.backgroundColor = UIColor.systemYellow

           [pic,move].forEach(
               $0.translatesAutoresizingMaskIntoConstraints = false
               self.view.addSubview($0)
           )

        oldCons = [
               pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
               pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -200),
               pic.widthAnchor.constraint(equalToConstant: CGFloat(box)),
               pic.heightAnchor.constraint(equalToConstant: 450)
            ]
           NSLayoutConstraint.activate(oldCons)

           NSLayoutConstraint.activate ([
               move.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :37.5),
               move.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
               move.widthAnchor.constraint(equalToConstant: 200),
               move.heightAnchor.constraint(equalToConstant: 50),

           ])
           move.addTarget(self, action: #selector(moveRight), for: .touchUpInside)
    

    @objc func moveRight() 
        NSLayoutConstraint.deactivate(oldCons)
        NSLayoutConstraint.activate ([
            pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :50),
            pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -50),
            pic.widthAnchor.constraint(equalToConstant: CGFloat(box)),
            pic.heightAnchor.constraint(equalToConstant: 450)
        ])

        box = Int(CGFloat(20 + move.value*90))
        print(box)
    

【问题讨论】:

【参考方案1】:

添加picWidthConstraint 作为类级属性,而不是激活/停用约束集:

    // we'll use this to dynamically change the pic width
    var picWidthConstraint: NSLayoutConstraint!

那么,当你想改变pic的宽度时,改变那个约束的.constant

    box = Int(CGFloat(20 + move.value*90))
    print(box)
    picWidthConstraint.constant = CGFloat(box)

这是您的原始课程,经过修改(参见 cmets):

import UIKit

class ViewController: UIViewController 

    var pic = UIImageView()
    var move = UISlider()

    // don't need this anymore
    //var oldCons = [NSLayoutConstraint]()

    var box = 20

    // we'll use this to dynamically change the pic width
    var picWidthConstraint: NSLayoutConstraint!

    override func viewDidAppear(_ animated: Bool) 
        super.viewDidAppear(animated)
        pic.backgroundColor = UIColor.systemYellow

        [pic,move].forEach(
            $0.translatesAutoresizingMaskIntoConstraints = false
            self.view.addSubview($0)
        )

        // create pic.widthAnchor property (var)
        picWidthConstraint = pic.widthAnchor.constraint(equalToConstant: CGFloat(box))

        NSLayoutConstraint.activate ([
            pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
            pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -200),

            // activate the pic width constraint
            picWidthConstraint,

            // don't create another one
            //pic.widthAnchor.constraint(equalToConstant: CGFloat(box)),

            pic.heightAnchor.constraint(equalToConstant: 450),

            // constraints for the slider
            move.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :37.5),
            move.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
            move.widthAnchor.constraint(equalToConstant: 200),
            move.heightAnchor.constraint(equalToConstant: 50),

        ])

        // use .valueChanged to change the pic width *while* dragging the slider thumb
//      move.addTarget(self, action: #selector(moveRight), for: .touchUpInside)
        move.addTarget(self, action: #selector(moveRight), for: .valueChanged)

    

    @objc func moveRight() 

        box = Int(CGFloat(20 + move.value*90))
        print(box)
        picWidthConstraint.constant = CGFloat(box)

    


【讨论】:

以上是关于如何以编程方式设置控制图像视图宽度的滑块的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIAppearance 渲染图像

使用滑块控制图像大小

如何使用 jQuery 中的滑块更改样式的值

如何为 iOS 设置包中的滑块指定暗/亮模式图像?

如何找到滑块的拇指来设置其宽度

如何以编程方式将 UIImageView 缩放到固定宽度和灵活高度?