如何为在单独函数中创建的 UIStackView 的元素设置动画

How to animate elements of a UIStackView that’s created in a separate function

我正在尝试在按下 UIButton 时为堆栈视图及其元素设置动画。问题是堆栈视图是在 viewdidload 中调用的函数内创建的。

因为堆栈视图是在函数内部创建的,所以我无法在管理动画的函数内部调用它...我想我做错了什么...

我的stackview创建函数

    fileprivate func variableContent() {
            let stackviewButtons = UIStackView(arrangedSubviews: [registerSerialNumberButton, dothislaterButton])
            stackviewButtons.axis = .vertical
            stackviewButtons.alignment = .fill
            stackviewButtons.spacing = 10

            let stackview = UIStackView(arrangedSubviews: [registerTitlelabel, registerdescriptionLabel, serialnumberInput, stackviewButtons])
            stackview.axis = .vertical
            stackview.spacing = 20
            stackview.setCustomSpacing(40, after: registerdescriptionLabel)
            stackview.setCustomSpacing(100, after: serialnumberInput)
            view.addSubview(stackview)
            stackview.anchor(top: nil, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 0)
            stackview.centerXInSuperview()
            stackview.centerYInSuperview()
        }

viewdidload

    override func viewDidLoad() {
        variableContent()
    }

您可以 return 函数中的堆栈视图,并将对它的引用作为实例变量保留。这样你以后就可以重复使用它了。

class Controller: UIViewController {
    var stackView: UIStackView!
    fileprivate func variableContent() -> UIStackView {
        let stackviewButtons = UIStackView(arrangedSubviews: [registerSerialNumberButton, dothislaterButton])
        stackviewButtons.axis = .vertical
        stackviewButtons.alignment = .fill
        stackviewButtons.spacing = 10

        let stackview = UIStackView(arrangedSubviews: [registerTitlelabel, registerdescriptionLabel, serialnumberInput, stackviewButtons])
        stackview.axis = .vertical
        stackview.spacing = 20
        stackview.setCustomSpacing(40, after: registerdescriptionLabel)
        stackview.setCustomSpacing(100, after: serialnumberInput)
        view.addSubview(stackview)
        stackview.anchor(top: nil, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 0)
        stackview.centerXInSuperview()
        stackview.centerYInSuperview()
        return stackview
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.stackView = variableContent()
    }
}

另一个选择是给它一个标签 (stackview.tag = 42) 并使用 self.view.viewWithTag(42)

恢复它

除了Marcio的回答,你也可以选择性声明stackView,在函数中设置:

class Controller: UIViewController {
    var stackview: UIStackView?
    fileprivate func variableContent() {
            let stackviewButtons = UIStackView(arrangedSubviews: [registerSerialNumberButton, dothislaterButton])
            stackviewButtons.axis = .vertical
            stackviewButtons.alignment = .fill
            stackviewButtons.spacing = 10

            self.stackview = UIStackView(arrangedSubviews: [registerTitlelabel, registerdescriptionLabel, serialnumberInput, stackviewButtons])
            stackview.axis = .vertical
            stackview.spacing = 20
            stackview.setCustomSpacing(40, after: registerdescriptionLabel)
            stackview.setCustomSpacing(100, after: serialnumberInput)
            view.addSubview(stackview)
            stackview.anchor(top: nil, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 0)
            stackview.centerXInSuperview()
            stackview.centerYInSuperview()
    }

    // Whatever your animating function is:
    func animateStackView() {
    guard let stackview = self.stackview else { return }
    // You now have access to your stackView for the rest of this function.
    }
}

注: 为了与你的问题保持一致,我在代码中将 UIStackView 的名称保留为 stackview,但根据 Swift 命名约定,您应该以驼峰命名法命名变量 (stackView)