Search code examples
iosswiftxcodevar

how to hide an image after seconds from its appearing ios


so i used this code to show my image


  class ViewController: UIViewController {

   @IBOutlet var imageView: UIImageView!
  @IBAction func ness(_ sender: Any) {
      // when button tapped this code will execute
      imageView.isHidden = false
    }

   override func viewDidLoad() {
       super.viewDidLoad()
       imageView.isHidden = true   
   }

}

now i want the image to be invisible after seconds from its appearing and i want to do that for multiple times, should i double the same code for the otehr image views and other buttons ?


Solution

  • You can use this function

    func hideImageAfterTime(time: CFTimeInterval , imageView: UIImageView) {
        DispatchQueue.main.asyncAfter(deadline: .now() + time) {
          imageView.isHidden = true
        }
      }
    

    how to use

    @IBAction func ness(_ sender: Any) {
          // when button tapped this code will execute
           hideImageAfterTime(time: 2, imageView: imageView)
        }