gpt4 book ai didi

swift - 如何为多个按钮设置相同的 IBAction?

转载 作者:行者123 更新时间:2023-11-30 10:47:00 26 4
gpt4 key购买 nike

我正在尝试将多个按钮链接到相同的IBAction,以运行类似但不同的代码。代码是将在另一个 View Controller 上单击的图像设置到按钮下的 UIImageView 中。所有按钮都链接到相同的 View Controller ,但具有不同的序列。

我试图写一个 if 语句,但我似乎没有写对。我已命名每个相应的 UIImage View :technologyImageViewTwotechnologyImageViewThree ...等

下面是我用于第一个按钮的代码,它与名为 technologyImageView 的相应 UIImageView 一起使用

@IBAction func setTechnology(segue:UIStoryboardSegue) {
dismiss(animated: true) {

if let technology = segue.identifier{
self.persona.technology = technology
self.technologyView.technologyImageView.image = UIImage(named: technology)
}

//animating scale up of image
let scaleUp = CGAffineTransform.init(scaleX: 0.1, y:0.1)
self.technologyView.technologyImageView.transform = scaleUp
self.technologyView.technologyImageView.alpha = 0

//animating bounce effect
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.7, options: [], animations: {
self.technologyView.technologyImageView.transform = .identity
self.technologyView.technologyImageView.alpha = 1
}, completion: nil)
}

我希望每个按钮都应该转到 Segued View Controller ,并且所选的图像将显示在相应的按钮下。例如,如果我单击“Technology 2”按钮并选择一个图像,该图像将显示在名为 technologyImageViewTwoUIImageview 中。

最佳答案

有几个选项可供您选择:

选项 1:

这个选项会更受欢迎,即使用组件上的标签属性,这将允许您在按钮被操作时识别按钮的索引。

https://developer.apple.com/documentation/uikit/uiview/1622493-tag

@IBAction func action(_ sender: Any) {
dismiss(animated: true) {
var imageView: UIImageView!
let index = (sender as? UIView)?.tag ?? 0

switch index {
case 1:
persona.technology = <#T##String#>
imageView = technologyView.technologyImageViewTwo
case 2:
persona.technology = <#T##String#>
imageView = technologyView.technologyImageViewThree
default:
persona.technology = <#T##String#>
imageView = technologyView.technologyImageView
}

if let technology = persona.technology {
imageView.image = UIImage(named: technology)
}

//animating scale up of image
let scaleUp = CGAffineTransform.init(scaleX: 0.1, y:0.1)
imageView.transform = scaleUp
imageView.alpha = 0

//animating bounce effect
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.7, options: [], animations: {
imageView.transform = .identity
imageView.alpha = 1
})
}
}

选项 2:

如果您想继续使用现有的代码,则可以使用 Segue 标识符,其中包含可以拆分的组件,使您能够更有效地识别它们:

segueIdentifier-1, segueIdentifier-2, segueIdentifier-3

func setTechnology(segue: UIStoryboardSegue) {
dismiss(animated: true) {

var imageView: UIImageView!
let identifierComponents = segue.identifier?.components(separatedBy: "-")
let index = Int(identifierComponents?.last ?? "0")

switch index {
case 1:
imageView = technologyView.technologyImageViewTwo
case 2:
imageView = technologyView.technologyImageViewThree
default:
imageView = technologyView.technologyImageView
}

if let technology = identifierComponents?.first {
self.persona.technology = technology
imageView.image = UIImage(named: technology)
}

//animating scale up of image
let scaleUp = CGAffineTransform.init(scaleX: 0.1, y:0.1)
imageView.transform = scaleUp
imageView.alpha = 0

//animating bounce effect
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.7, options: [], animations: {
imageView.transform = .identity
imageView.alpha = 1
})
}
}

关于swift - 如何为多个按钮设置相同的 IBAction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55557851/

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