我遇到一个奇怪的问题。
我用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更改。甚至在第一次点击时都无法更改标签颜色。
复制步骤: