xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

SwiftUI 2024 All In One

SwiftUI 2024 All In One

WWDC24 SwiftUI & UI Frameworks guide

https://developer.apple.com/news/?id=zqzlvxlm

Swift 6

Swift 6.0.3

https://www.swift.org/install


struct ScientificName {
    var genus: String
    var species: String
    var subspecies: String?

    var description: String {
        var text = "\(genus) \(species)"
        if let subspecies {
            // subspecies guaranteed to be non-nil
            text += "subsp. \(subspecies)"
        }
        return text
    }
}

https://www.swift.org/

https://www.swift.org/migration/documentation/migrationguide/

https://developer.apple.com/swift/

demos

$ swift --version
Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: x86_64-apple-darwin23.2.0

image

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour/

Design and build your apps like never before. / 以前所未有的方式设计和构建您的应用程序。

With enhancements to live previews in Xcode, new customization options for animations and styling, and updates to interoperability with UIKit and AppKit views, SwiftUI is the best way to build apps for Apple platforms.
Dive into the latest sessions to discover everything new in SwiftUI, UIKit, AppKit, and more.
Make your app stand out with more options for custom visual effects and enhanced animations.
And explore sessions that cover the essentials of building apps with SwiftUI.

SwiftUI 增强了 Xcode 中的实时预览功能,增加了动画和样式自定义选项,并更新了与 UIKit 和 AppKit 视图的互操作性,是构建适用于 Apple 平台的应用的最佳方式。
深入了解最新会议,了解 SwiftUI、UIKit、AppKit 等的所有新功能。
通过更多自定义视觉效果增强动画选项让您的应用脱颖而出。
并探索涵盖使用 SwiftUI 构建应用的基本知识的会议。

image

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

iOS 18

@MainActor

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .onAppear(perform: doSampleWork)
    }

    func doSampleWork() {
        Task {
            for i in 1...10_000 {
                print("Work 1-\(i)")
            }
        }

        Task {
            for i in 1...10_000 {
                print("Work 2-\(i)")
            }
        }
    }
}

https://www.hackingwithswift.com/articles/270/whats-new-in-swiftui-for-ios-18

SwiftUI introduced the new overloads for Group and ForEach views, allowing us to create custom containers like List or TabView.

struct AppStoreView<Content: View>: View {
    @ViewBuilder var content: Content
    
    var body: some View {
        VStack {
            Group(subviewsOf: content) { subviews in
                HStack {
                    if !subviews.isEmpty {
                        subviews[0]
                    }
                    if subviews.count > 1 {
                        subviews[1]
                    }
                }
                if subviews.count > 2 {
                    VStack {
                        subviews[2...]
                    }
                }
            }
        }
    }
}

Using the new Tab type, the new customizable tab bar experience with fluid transition into a sidebar is available in SwiftUI.

enum Destination: Hashable {
    case home
    case search
    case settings
    case trends
}

struct RootView: View {
    @State private var selection: Destination = .home
    
    var body: some View {
        TabView {
            Tab("home", systemImage: "home", value: .home) {
                HomeView()
            }
            
            Tab("search", systemImage: "search", value: .search) {
                SearchView()
            }
            
            TabSection("Other") {
                Tab("trends", systemImage: "trends", value: .trends) {
                    TrendsView()
                }
                Tab("settings", systemImage: "settings", value: .settings) {
                    SettingsView()
                }
            }
            .tabViewStyle(.sidebarAdaptable)
        }
    }
}

SwiftUI introduced matchedTransitionSource and navigationTransition, which we can use in pair on any instance of the NavigationLink type.

struct HeroAnimationView: View {
    @Namespace var hero
    
    var body: some View {
        NavigationStack {
            NavigationLink {
                DetailView()
                    .navigationTransition(.zoom(sourceID: "myId", in: hero))
            } label: {
                ThumbnailView()
            }
            .matchedTransitionSource(id: "myId", in: hero)
        }
    }
}

The new ScrollPosition type, in pair with the scrollPosition view modifier, allows us to read the precise position of a ScrollView instance. We can also use it to programmatically scroll to the particular point of the scrolling content.

struct ScrollPositionExample: View {
    @State private var position: ScrollPosition = .init(point: .zero)
    
    var body: some View {
        ScrollView {
            ForEach(1..<1000) { item in
                Text(item.formatted())
            }
            
            Button("jump to top") {
                position = ScrollPosition(point: .zero)
            }
        }
        .scrollPosition($position)
    }
}

The new Entry macro allows us to quickly introduce environment values, focused values, container values, etc, without boilerplate.
Let’s look at how we define environment values before the Entry macro.

struct ItemsPerPageKey: EnvironmentKey {
    static var defaultValue: Int = 10
}

extension EnvironmentValues {
    var itemsPerPage: Int {
        get { self[ItemsPerPageKey.self] }
        set { self[ItemsPerPageKey.self] = newValue }
    }
}

Now, we can minimize our code by using the Entry macro.

extension EnvironmentValues {
    @Entry var itemsPerPage: Int = 10
}

The new Previewable macro allows us to introduce the state to our previews without wrapping it into additional wrapper-view.

#Preview("toggle") {
    @Previewable @State var toggled = true
    return Toggle("Loud Noises", isOn: $toggled)
}

https://swiftwithmajid.com/2024/06/10/what-is-new-in-swiftui-after-wwdc24/

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2024-12-23 17:33  xgqfrms  阅读(0)  评论(2编辑  收藏  举报