xcode 在 Swift 中显示/隐藏图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37569037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show / Hide Image in Swift
提问by Xeven Elewen
how to show or hide a image in Swift by taping on a Button? For example:
如何通过点击按钮在 Swift 中显示或隐藏图像?例如:
i have ImageA and ImageB and one Button which i want to use to move ImageA to ImageB and back to ImageA and so on..
我有 ImageA 和 ImageB 和一个按钮,我想用它来将 ImageA 移动到 ImageB 并返回到 ImageA 等等。
It works perfect to move from ImageA to ImageB, but how can i move back to ImageA?
从 ImageA 移动到 ImageB 非常有效,但我如何才能移回 ImageA?
My code is:
我的代码是:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Bild1: UIImageView!
@IBOutlet weak var Bild2: UIImageView!
@IBAction func pressedButton(sender: AnyObject) {
Bild1.hidden = true
Bild2.hidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
Bild1.hidden = false
Bild2.hidden = true
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答by Jayesh Miruliya
@IBAction func pressedButton(sender: AnyObject)
{
if Bild2.tag == 0
{
Bild1.hidden = true
Bild2.hidden = false
Bild2.tag=1
}
else
{
Bild1.hidden = false
Bild2.hidden = true
Bild2.tag=0
}
}
override func viewDidLoad()
{
super.viewDidLoad()
Bild1.hidden = false
Bild2.hidden = true
Bild2.tag=0
// Do any additional setup after loading the view, typically from a nib.
}
回答by Sabyasachi Ghosh
@IBAction func pressedButton(sender: AnyObject) {
Bild1.isHidden = !Bild1.isHidden
Bild2.isHidden = !Bild2.isHidden
}
This will toggle the image property between show and hidden. Syntax is Swift4 compatible
这将在显示和隐藏之间切换图像属性。语法与 Swift4 兼容
回答by Polis
try replacing with:
尝试替换为:
@IBAction func pressedButton(sender: AnyObject) {
if Bild1.hidden {
Bild1.hidden = false
Bild2.hidden = true
} else {
Bild1.hidden = true
Bild2.hidden = false
}
}
回答by Shadow Of
@IBAction func pressedButton(sender: AnyObject) {
Bild1.hidden = !Bild1.hidden
Bild2.hidden = !Bild2.hidden
}