iT邦幫忙

2023 iThome 鐵人賽

DAY 18
0
Mobile Development

SwiftUI 男孩系列 第 18

Day 18: ~~不義遊戲~~跳轉頁面,逆轉!

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20231002/201301389ezw6LW3ql.jpg
Photo by Tom Sakai on Unsplash
Mid-Winter Queenstown, New Zealand

Press ‘Command + N’ to create a new Swift file named ‘GlobalSettings’

class GlobalSettings: ObservableObject {
    @Published var selectedTabIndex: Int = 0
}

這是使用SwiftUI的基本方式來創建一個可觀察的物件。這意味著當 selectedTabIndex 的值更改時,任何觀察此物件的視圖將會重新渲染。

要使用 GlobalSettings

  1. 在你的某個視圖中,創建一個這個類的實例:

    @ObservedObject var settings = GlobalSettings()
    
    
  2. 然後,你可以使用 settings.selectedTabIndex 並根據需要進行修改。當 selectedTabIndex 改變時,視圖會自動更新。

在 ContentView

struct ContentView: View {
    @ObservedObject var settings = GlobalSettings()

TabView 裡新增

SettingsView().tabItem {
    NavigationLink(destination: SettingsView()) {
        Label("Settings", systemImage: "gearshape")
    }
}.tag(4)
FourTargetButtonView(settings: settings).tabItem {
    NavigationLink(destination: FourTargetButtonView(settings: settings)) {
        Label("hands", systemImage: "hands.sparkles")
    }
}.tag(5)

Tips: tab bar 最多5個,不要太貪心😙

https://ithelp.ithome.com.tw/upload/images/20231002/201301382eI6QEwORh.png

struct FourTargetButtonView: View {
    @ObservedObject var settings: GlobalSettings
    var body: some View {
        VStack(spacing: 20) {
            HStack(spacing: 20) {
                TargetButtonView(iconName: "house", tagNumber: 1, settings: settings)
                TargetButtonView(iconName: "list.clipboard.fill", tagNumber: 2, settings: settings)
            }
            
            HStack(spacing: 20) {
                TargetButtonView(iconName: "map", tagNumber: 3, settings: settings)
                TargetButtonView(iconName: "gearshape", tagNumber: 4, settings: settings)
            }
        }
        .padding(.horizontal, 20)
    }
}

https://ithelp.ithome.com.tw/upload/images/20231002/20130138TNu4GIXffd.png

struct TargetButtonView: View {
    var iconName: String
    var tagNumber: Int
    @ObservedObject var settings: GlobalSettings
    
    var buttonSize: CGFloat {
        return (UIScreen.main.bounds.width - 60) / 2
    }
    
    var body: some View {
        Button(action: {
            // 在此處放置按鈕的動作
            settings.selectedTabIndex = tagNumber
        }) {
            Image(systemName: iconName)
                .resizable()  // 使圖標可調整大小
                .frame(width: buttonSize - 80, height: buttonSize - 80)  // 設定按鈕的寬度和高度以保持正方形,減去左右各40像素的間距
                .padding(40)  // 為圖標增加40像素的間距
                .background(RoundedRectangle(cornerRadius: buttonSize / 4).fill(Color.blue)) // 等比例圓角
                .foregroundColor(.white)
        }
    }
}

Yes

https://ithelp.ithome.com.tw/upload/images/20231002/20130138I0UvJJHjfw.jpg

不義遊戲跳轉頁面完成

👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻逃不出我的手掌心吧

什麼你說 @ObservedObject 會搞的很多層傳值,此案例就傳三層,有更好的解法!
沒錯,埋下這伏筆
這就是我給你的試煉
https://ithelp.ithome.com.tw/upload/images/20231002/20130138HWDJVmeZ4a.png

提示:@StateObject or @EnvironmentObject

燃燒你的熱情
下集待續 💪🏻


上一篇
Day 17: 有 bug 劇情反轉!!!
下一篇
Day 19: Codable, Equatable, Identifiable
系列文
SwiftUI 男孩30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言