Show / Hide Image in Swift

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-23 12:26:26

问题


how to show or hide a image in Swift by taping on a Button? For example:

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..

It works perfect to move from ImageA to ImageB, but how can i move back to 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.
}


}

回答1:


@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.
}



回答2:


@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




回答3:


try replacing with:

@IBAction func pressedButton(sender: AnyObject) {
    if Bild1.hidden {
        Bild1.hidden = false
        Bild2.hidden = true
    } else {
        Bild1.hidden = true
        Bild2.hidden = false
    } 
}



回答4:


@IBAction func pressedButton(sender: AnyObject) {

    Bild1.hidden = !Bild1.hidden
    Bild2.hidden = !Bild2.hidden
}


来源:https://stackoverflow.com/questions/37569037/show-hide-image-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!