如何通过点击按钮在Swift中显示或隐藏图像? 例如:
我有ImageA和ImageB以及一个我想用来将ImageA移动到ImageB并返回ImageA的按钮等等。
从ImageA转移到ImageB是完美的,但我怎样才能回到ImageA?
我的代码是:
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.
}
}
答案 0 :(得分:5)
@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.
}
答案 1 :(得分:3)
@IBAction func pressedButton(sender: AnyObject) {
Bild1.isHidden = !Bild1.isHidden
Bild2.isHidden = !Bild2.isHidden
}
这将切换show和hidden之间的image属性。 语法是兼容Swift4的
答案 2 :(得分:0)
尝试替换为:
@IBAction func pressedButton(sender: AnyObject) {
if Bild1.hidden {
Bild1.hidden = false
Bild2.hidden = true
} else {
Bild1.hidden = true
Bild2.hidden = false
}
}
答案 3 :(得分:0)
@IBAction func pressedButton(sender: AnyObject) {
Bild1.hidden = !Bild1.hidden
Bild2.hidden = !Bild2.hidden
}