iOS Swift 4 UIView被tapGestureRecognizer隐藏

时间:2018-11-04 09:48:30

标签: ios swift uiview uiviewcontroller

我遇到一个奇怪的问题。

我用TabBarController制作了一个简单的Single View App,将子视图添加到第一个项目(ViewController)中,并使用界面生成器属性将其隐藏。

现在,我想在整个视图中识别出水龙头,以使其一次又一次地可见,并在最后将其再次隐藏。

gestureRecogniser是从界面构建器放置的。所有插座均已正确连接并可以正常工作。

无论如何,当我第一次点击UIView不会显示,但是isHidden属性设置正确。我尝试了很多事情并进行了搜索,但是在这种情况下并没有什么真正的帮助。将所有内容强制包装到主线程中也无济于事,在这种情况下也不应有所作为。

这是我的代码:

//
//  ViewController.swift
//

import UIKit

class ViewController: UIViewController {

  var step = 0

  @IBOutlet weak var firstViewOutlet: UIView! // properly connected
  @IBOutlet weak var secondViewOutlet: UIView! // properly connected
  @IBOutlet weak var thirdViewOutlet: UIView! // properly connected
  @IBOutlet var tapRecognizer: UITapGestureRecognizer! // properly connected

  override func viewDidLoad() {
    super.viewDidLoad()
    self.tapRecognizer.addTarget(self, action: #selector (self.tapAction(_:)))
    print("viewDidLoad")
    // Tried to hide them manually on load. Didn't work so I switched back to the interface builder property (checkbox)
    // hideSubviews()
  }
  override func viewDidLayoutSubviews() {
    print("viewDidLayoutSubviews")
  }

  @objc func tapAction(_ sender:UITapGestureRecognizer){
    print("Tapped: \(self.step)") // Always works and shows the correct step value
    switch self.step {
    case 0:
      print("Untap should show! \(self.step)") // Shows after first tap
      DispatchQueue.main.async {
        self.firstViewOutlet.isHidden = false
        print("Value of first view state: \(self.firstViewOutlet.isHidden)") // shows false but visible after SECOND tap
        self.step += 1
      }

      // Alternative doesn't change anything
      // self.firstViewOutlet.isHidden = false

    case 1:
      print("Upkeep should show! \(self.step)") // Shows second first tap
      DispatchQueue.main.async {
        self.secondViewOutlet.isHidden = false
        print("Value of second view state: \(self.secondViewOutlet.isHidden)") // shows false but visible after THIRD tap
        self.step += 1
      }

      // Alternative doesn't change anything
      // self.secondViewOutlet.isHidden = false

    case 2:
      print("Draw should show! \(self.step)") // Shows after third tap
      DispatchQueue.main.async {
        self.thirdViewOutlet.isHidden = false
        print("Value of third view state: \(self.thirdViewOutlet.isHidden)") // shows false but visible after FOURTH tap
        self.step += 1
      }

      // Alternative doesn't change anything
      // self.thirdViewOutlet.isHidden = true

    case 3:
      print("Draw should clear! \(self.step)")
      hideSubviews() // works after FIFTH tap

      // Alternative doesn't change anything
      // self.firstViewOutlet.isHidden = true
      // self.secondViewOutlet.isHidden = true
      // self.thirdViewOutlet.isHidden = true
      // self.step = 0
    default:
      return
    }
  }

  func hideSubviews(){
    DispatchQueue.main.async {
      self.thirdViewOutlet.isHidden = true
      self.firstViewOutlet.isHidden = true
      self.secondViewOutlet.isHidden = true
      self.step = 0
    }
  }
}

很有趣,在第一个cicle之后,一切都按预期运行。 我什至尝试将view.layoutIfNeeded()view.layoutSubviews()view.setNeedsDisplay()view.setNeedsLayout()添加到销售点和self.view,但也没有任何改变。

任何建议都值得赞赏。我敢打赌,这是一个我看不见的小而愚蠢的错误。

似乎是任何UI更改。甚至在第一次点击时都无法更改标签颜色。

复制步骤:

  • 创建一个新项目(单视图应用程序)。
  • 单击ViewController,然后在Editor-> Embed in-> Tap Bar Controller中
  • 单击“库”图标,将“容器”视图拖动到ViewController
  • 在Interface Builder中将此容器视图的[hidden]属性设置为true
  • 从容器视图创建插座到ViewController
  • 单击“库”图标,将“点击手势识别器”拖到ViewController(不拖到容器视图中!)
  • 从容器视图创建插座到ViewController
  • 复制我的代码或从头开始编写

0 个答案:

没有答案