0
我在視圖控制器的另一個頂部有很多視圖,我試圖模擬一個標籤欄。用戶可以點擊菜單中的任何按鈕,並根據點擊哪個按鈕來呈現視圖。所有其他視圖將隱藏。在iOS中模擬一個標籤欄
我寫了下面的代碼,它沒有正確呈現視圖(有時會顯示出來,其他時間則不會)。
presentViewWithName(.home) // this will present the view at the very bottom of the hierarchy
enum Views {
case home
case sketchpad
case help
case settings
}
func presentViewWithName(name: Views) {
self.turnAlphaOnToEveryMenuButton()
switch name {
case .home:
self.hideAllViews()
self.menuView.buttonHome.dimmerAlpha()
case .sketchpad:
self.sketchpadView.animateToggleAlpha()
self.menuView.buttonSketchpad.dimmerAlpha()
case .help:
self.helpView.animateToggleAlpha()
self.menuView.buttonHelp.dimmerAlpha()
case .settings:
self.settingsView.animateToggleAlpha()
self.menuView.buttonSettings.dimmerAlpha()
}
}
func turnAlphaOnToEveryMenuButton() {
self.menuView.buttonHome.alpha = 1
self.menuView.buttonSketchpad.alpha = 1
self.menuView.buttonHelp.alpha = 1
self.menuView.buttonSettings.alpha = 1
}
func hideAllViews() {
self.settingsView.alpha = 0
self.sketchpadView.alpha = 0
self.helpView.alpha = 0
}
extension UIView {
func animateToggleAlpha() {
UIView.animateWithDuration(0.25) {
self.alpha = self.alpha == 1 ? 0 : 1
}
}
func dimmerAlpha() {
UIView.animateWithDuration(0.25) {
self.alpha = self.alpha == 1 ? 0.4 : 1
}
}
}
我基本上改變了視圖的alpha。假設您想呈現主視圖。這是層次結構最底層的觀點。我只是隱藏所有的意見。假設我想在層次結構的頂部,即設置視圖中呈現視圖。我隱藏所有視圖,然後顯示設置視圖。
我該如何解決這個/更好地做到這一點?
很好的答案,很好的一個 – Fattie