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
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/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
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 构建应用的基本知识的会议。
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 value
s, 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-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18624615
未经授权禁止转载,违者必究!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
2022-12-23 Web 安全问题验证:如何使用 CSRF 窃取 token/cookie All In One
2021-12-23 how to stop linux terminal echo disappeared quickly All in One
2021-12-23 npm script 自动打开浏览器 All In One
2021-12-23 vue provide inject 作用域测试,多个provide 同名覆盖
2021-12-23 html5 template All In One
2020-12-23 CSS Grid & Flex poster PDF 海报定制 All In One
2020-12-23 H5 CSS 悬浮滚动条 All In One