如果在if语句中满足条件,则不隐藏UIView

时间:2017-02-27 02:46:39

标签: swift uiview

我正在尝试创建动画并在任务完成后5秒隐藏它。 出于某种原因,overlayView时屏幕上未隐藏counter == 5

 var counter = 0
 var timer = Timer()
 let paymentLogo = UIImage(named: "paymentImage")
 var imageLogo:UIImageView!
 var overlayView = UIView()
 var logoAppeared:Bool? = false
 let labelLogo = UILabel()


  override func viewDidLayoutSubviews() {
     //move picture off screen
     animateDidLayout()
  }


  override func viewDidAppear(_ animated: Bool) {
   //move picture on screen and adjust view
     animateDidAppear()
 }

//called by timer every 1 seconds
  func startCounting() {
     if counter == 5 {
        self.timer.invalidate()
         self.overlayView.isHidden = true //it is not hidden
          self.imageLogo.isHidden = true
           self.labelLogo.isHidden = true
             self.logoAppeared = true
                 print("counter in if \(counter)")
         } else {
           counter += 1
            print("counter in else \(counter)")
     }
 }


    //animate the image before it appears on screen
       func animateDidLayout(){
          guard let appeared = self.logoAppeared, appeared == false else{
            print("appeared is true in viewDidLayoutSubviews, in else")
             return
     }

     print("appeared value after else in ViewDidLayoutSubvies \(appeared)")

     //animate overlayView
     self.overlayView = UIView(frame: self.view.frame)
      self.overlayView.backgroundColor = UIColor.black
       self.overlayView.alpha = 0.4
        self.view.addSubview(self.overlayView)


   //animate imageLogo
     self.imageLogo = UIImageView(image:paymentLogo)
      imageLogo.frame = CGRect(x: 0, y: 0, width: 100,
      height: 100)
      imageLogo.center.x -= 400
         self.view.addSubview(imageLogo)

 //animate labelLogo
    self.labelLogo.frame =
        CGRect(x: 0, y: 0, width: 200, height: 21)
    self.labelLogo.center.x -= 400
    self.labelLogo.text = "Your booking is confirmed!"
    self.labelLogo.textAlignment = .center
        self.view.addSubview(labelLogo)
 }//end of animateDidLayout






 //animate the logo when the view has appeared
  //call it in ViewDidAppear

   func animateDidAppear() {
       guard let appeared = self.logoAppeared, appeared == false else{
         print("appeared is true in viewDidAppear, in else")
           return
 }

     print("appeared value in viewDidAppear  after else \(appeared)")

    UIView.animate(withDuration: 1.0, delay: 0.1, options: [],
                   animations: {

     //animate overlayView
    // self.overlayView = UIView(frame: self.view.frame)
    //self.overlayView.backgroundColor = UIColor.black
    //self.overlayView.alpha = 0.4


  //animate labelLogo
   self.labelLogo.frame = CGRect(x: self.view.center.x - 100,    
    y: 340, width: 200, height: 21)

   self.labelLogo.text = "Your booking is confirmed!"
   self.labelLogo.textColor = .white
   self.labelLogo.textAlignment = .center
   self.labelLogo.sizeToFit()

    //animate imageLogo
     self.imageLogo.frame =
     CGRect(x: self.view.center.x - 50,y: 250,width: 100,height: 90)

       }) { finished in

        self.timer = Timer.scheduledTimer(timeInterval: 1, target: self,    
  selector: #selector(ThirteenthViewController.startCounting), userInfo: nil,      
    repeats: true)  
     }
} //end of animateDidAppear

overlayView not hidden

1 个答案:

答案 0 :(得分:1)

我要么使用延迟,要么使用另一个有延迟的动画来删除隐藏视图。我不会依赖计时器。使用代码删除动画并删除计时器的示例。

class ViewController: UIViewController {
var counter = 0
let paymentLogo = UIImage(named: "paymentImage")
var imageLogo:UIImageView!
var overlayView = UIView()
var logoAppeared:Bool? = false
let labelLogo = UILabel()


override func viewDidLayoutSubviews() {
    //move picture off screen
    animateDidLayout()
}


override func viewDidAppear(_ animated: Bool) {
    //move picture on screen and adjust view
    animateDidAppear()
}



//animate the image before it appears on screen
func animateDidLayout(){
    guard let appeared = self.logoAppeared, appeared == false else{
        print("appeared is true in viewDidLayoutSubviews, in else")
        return
    }

    print("appeared value after else in ViewDidLayoutSubvies \(appeared)")

    //animate overlayView
    if self.view.subviews.contains(overlayView) != true{
        self.overlayView = UIView(frame: self.view.frame)
        self.view.addSubview(self.overlayView)
    }

    self.overlayView.backgroundColor = UIColor.black
    self.overlayView.layer.opacity = 0.4
    self.overlayView.alpha = 0.4



    //animate imageLogo
    if self.view.subviews.contains(imageLogo) != true{
        self.imageLogo = UIImageView(image:paymentLogo)
        self.view.addSubview(imageLogo)
    }

    imageLogo.frame = CGRect(x: 0, y: 0, width: 100,
                             height: 100)
    imageLogo.center.x -= 400


    //animate labelLogo
    self.labelLogo.frame =
        CGRect(x: 0, y: 0, width: 200, height: 21)
    self.labelLogo.center.x -= 400
    self.labelLogo.text = "Your booking is confirmed!"
    self.labelLogo.textAlignment = .center
    if self.view.subviews.contains(labelLogo) != true{
        self.view.addSubview(labelLogo)
    }

}//end of animateDidLayout







//animate the logo when the view has appeared
//call it in ViewDidAppear

func animateDidAppear() {
    guard let appeared = self.logoAppeared, appeared == false else{
        print("appeared is true in viewDidAppear, in else")
        return
    }

    print("appeared value in viewDidAppear  after else \(appeared)")

    UIView.animate(withDuration: 1.0, delay: 0.1, options: [],
                   animations: {

                    //animate overlayView
                    // self.overlayView = UIView(frame: self.view.frame)
                    //self.overlayView.backgroundColor = UIColor.black
                    //self.overlayView.alpha = 0.4


                    //animate labelLogo
                    self.labelLogo.frame = CGRect(x: self.view.center.x - 100,
                                                  y: 340, width: 200, height: 21)

                    self.labelLogo.text = "Your booking is confirmed!"
                    self.labelLogo.textColor = .white
                    self.labelLogo.textAlignment = .center
                    self.labelLogo.sizeToFit()

                    //animate imageLogo
                    self.imageLogo.frame =
                        CGRect(x: self.view.center.x - 50,y: 250,width: 100,height: 90)

    }) { finished in

    }

    UIView.animate(withDuration: 0.5, delay: 5.0, options: .curveEaseInOut, animations: {
        self.overlayView.alpha = 0
        self.imageLogo.alpha = 0
        self.labelLogo.alpha = 0

    }, completion: {
        finished in
        self.overlayView.isHidden = true //it is hidden :)
        self.imageLogo.isHidden = true
        self.labelLogo.isHidden = true
        self.logoAppeared = true
    })

} //end of animateDidAppear}

如果您希望等待5秒钟并且不使用动画进行隐藏,则可以使用

let when = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: when) {// Your code with delay}

或者使用您的代码和延迟作为一个非常有趣的替代方案

class ViewController: UIViewController {
var counter = 0
let paymentLogo = UIImage(named: "paymentImage")
var imageLogo:UIImageView!
var overlayView = UIView()
var logoAppeared:Bool? = false
let labelLogo = UILabel()


override func viewDidLayoutSubviews() {
    //move picture off screen
    animateDidLayout()
}


override func viewDidAppear(_ animated: Bool) {
    //move picture on screen and adjust view
    animateDidAppear()
}



//animate the image before it appears on screen
func animateDidLayout(){
    guard let appeared = self.logoAppeared, appeared == false else{
        print("appeared is true in viewDidLayoutSubviews, in else")
        return
    }

    print("appeared value after else in ViewDidLayoutSubvies \(appeared)")

    //animate overlayView
    if self.view.subviews.contains(overlayView) != true{
        self.overlayView = UIView(frame: self.view.frame)
        self.view.addSubview(self.overlayView)
    }

    self.overlayView.backgroundColor = UIColor.black
    self.overlayView.layer.opacity = 0.4
    self.overlayView.alpha = 0.4



    //animate imageLogo
    if self.view.subviews.contains(imageLogo) != true{
        self.imageLogo = UIImageView(image:paymentLogo)
        self.view.addSubview(imageLogo)
    }

    imageLogo.frame = CGRect(x: 0, y: 0, width: 100,
                             height: 100)
    imageLogo.center.x -= 400


    //animate labelLogo
    self.labelLogo.frame =
        CGRect(x: 0, y: 0, width: 200, height: 21)
    self.labelLogo.center.x -= 400
    self.labelLogo.text = "Your booking is confirmed!"
    self.labelLogo.textAlignment = .center
    if self.view.subviews.contains(labelLogo) != true{
        self.view.addSubview(labelLogo)
    }

}//end of animateDidLayout







//animate the logo when the view has appeared
//call it in ViewDidAppear

func animateDidAppear() {
    guard let appeared = self.logoAppeared, appeared == false else{
        print("appeared is true in viewDidAppear, in else")
        return
    }

    print("appeared value in viewDidAppear  after else \(appeared)")

    UIView.animate(withDuration: 1.0, delay: 0.1, options: [],
                   animations: {

                    //animate overlayView
                    // self.overlayView = UIView(frame: self.view.frame)
                    //self.overlayView.backgroundColor = UIColor.black
                    //self.overlayView.alpha = 0.4


                    //animate labelLogo
                    self.labelLogo.frame = CGRect(x: self.view.center.x - 100,
                                                  y: 340, width: 200, height: 21)

                    self.labelLogo.text = "Your booking is confirmed!"
                    self.labelLogo.textColor = .white
                    self.labelLogo.textAlignment = .center
                    self.labelLogo.sizeToFit()

                    //animate imageLogo
                    self.imageLogo.frame =
                        CGRect(x: self.view.center.x - 50,y: 250,width: 100,height: 90)

    }) { finished in

    }

     let when = DispatchTime.now() + 5
    DispatchQueue.main.asyncAfter(deadline: when) {
        let transition = CATransition()
        transition.duration = 0.5
        transition.type = "suckEffect"
        self.view.layer.add(transition, forKey: nil)
        self.overlayView.isHidden = true //it is hidden :)
        self.imageLogo.isHidden = true
        self.labelLogo.isHidden = true
        self.logoAppeared = true
    }

} //end of animateDidAppear}