gpt4 book ai didi

ios - 如何访问父类(super class)初始化期间定义的属性

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:23 24 4
gpt4 key购买 nike

我正在创建一个带有 2 个 ImageView 的自定义 View ,我希望能够从一个 ImageView 到另一个 ImageView 进行动画处理,这是我的代码:

class FlashCard: UIView {

required init?(coder: NSCoder) {
super.init(coder: coder)

let firstImageView = UIImageView(frame: self.frame)
firstImageView.image = UIImage(named: "Lightning")
addSubview(firstImageView)

let secondImageView = UIImageView(frame: frame)
secondImageView.image = UIImage(named: "Tifa")
addSubview(secondImageView)
secondImageView.isHidden = true

}

func showBack() {
UIView.transition(from: firstImageView, to: secondImageView, duration: 1.0, options: UIViewAnimationOptions.transitionCrossDissolve, completion: nil)
}
}

但是在我的 showBack 函数中,我得到了错误:使用未解析的标识符。

如何正确访问 firstImageViewsecondImageView

最佳答案

ImageView 不需要是变量,无论是什么类型的可选。

class FlashCard: UIView {

let firstImageView = UIImageView()
let secondImageView: UIImageView()

required init?(coder: NSCoder) {
super.init(coder: coder)

firstImageView.frame = frame
firstImageView.image = UIImage(named: "Lightning")
addSubview(firstImageView)

secondImageView.frame = frame
secondImageView.image = UIImage(named: "Tifa")
addSubview(secondImageView)
secondImageView.isHidden = true
}

func showBack() {
UIView.transition(from: firstImageView, to: secondImageView, duration: 1.0, options: UIViewAnimationOptions.transitionCrossDissolve, completion: nil)
}
}

类的属性必须在 super 初始化器被调用之前初始化,这就是为什么另一个答案使用隐式展开的可选变量。

关于ios - 如何访问父类(super class)初始化期间定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39286862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com