- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将多个按钮链接到相同的IBAction
,以运行类似但不同的代码。代码是将在另一个 View Controller 上单击的图像设置到按钮下的 UIImageView
中。所有按钮都链接到相同的 View Controller ,但具有不同的序列。
我试图写一个 if 语句,但我似乎没有写对。我已命名每个相应的 UIImage
View :technologyImageViewTwo
、technologyImageViewThree
...等
下面是我用于第一个按钮的代码,它与名为 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”按钮并选择一个图像,该图像将显示在名为 technologyImageViewTwo
的 UIImageview
中。
最佳答案
有几个选项可供您选择:
选项 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/
我是一名优秀的程序员,十分优秀!