当前位置:   article > 正文

SwiftUI初探

SwiftUI初探

      SwiftUI 虽然出现了好几年(1.0好像2019年出的,还有SPM也是同一年),现在已经到从1.0到5.0,但受限于对系统的要求(最低iOS13.0,有的要求17.0及以上),每个版本里面差异也很大,语法和Flutter 的Dart 比较像。空闲之余可以先学习下,给有需要的同学做个参考, 官方也有现成案例,展示效果如图:

地标列表:

  1. import SwiftUI
  2. /// 列表
  3. struct LandmarkList : View {
  4. @Environment(ModelData.self) var modelData
  5. /// 仅展示收藏
  6. @State private var showFavoritesOnly = false
  7. /// 数据过滤
  8. private var filterLandmarks:[Landmark] {
  9. modelData.landmarkData.filter { $0.isFavorite == true || !showFavoritesOnly }
  10. }
  11. /// 转场设置
  12. @State private var selectIndex:Int = 0
  13. @State private var _index:Int? = nil
  14. var body: some View {
  15. NavigationView {
  16. List{
  17. Toggle(isOn: $showFavoritesOnly, label: {
  18. Text("Favorites Only")
  19. })
  20. if selectIndex == 0 {
  21. ForEach(filterLandmarks) { landmark in
  22. NavigationLink {
  23. LandMarkDetail(landmark: landmark)
  24. } label: {
  25. LandmarkRow(landmark: landmark)
  26. }
  27. }
  28. }
  29. else{
  30. ForEach(filterLandmarks.indices,id:\.self) { index in
  31. LandmarkRow(landmark: filterLandmarks[index])
  32. .onTapGesture {
  33. _index = index
  34. debugPrint("onTapGesture:{\(index),\(String(describing: _index))}")
  35. }
  36. }
  37. .sheet(isPresented: .constant(selectIndex > 0 && _index != nil),
  38. onDismiss: {
  39. _index = nil
  40. },
  41. content: {
  42. if _index != nil && filterLandmarks.count > _index! {
  43. LandMarkDetail(landmark: filterLandmarks[_index!])
  44. }
  45. })
  46. }
  47. }
  48. .navigationBarTitle("Landmarks", displayMode: .large)
  49. .navigationBarItems(trailing: SegmentButton(selectIndex:$selectIndex))
  50. .animation(.easeInOut, value: 0.5)
  51. }
  52. }
  53. }
  54. #Preview {
  55. let modelData = ModelData()
  56. return LandmarkList()
  57. .environment(modelData)
  58. }

详情:

  1. import SwiftUI
  2. struct LandMarkDetail : View {
  3. @Environment(ModelData.self) var modelData
  4. var landmark : Landmark
  5. var landmarkIndex:Int? {
  6. get{
  7. modelData.landmarkData.firstIndex { $0.id == landmark.id }
  8. }
  9. }
  10. var body: some View {
  11. @Bindable var modelData = modelData
  12. ScrollView {
  13. MapView(coordinate: landmark.locationCoordinate).frame(height: 300)
  14. CircleImage(image: landmark.image(forSize: 250))
  15. .offset(y: -130)
  16. .padding(.bottom, -130)
  17. VStack(alignment: .leading) {
  18. HStack{
  19. Text(landmark.name)
  20. .font(.title)
  21. FavoriteButton(isSet: landmarkIndex != nil ? $modelData.landmarkData[landmarkIndex!].isFavorite : .constant(false))
  22. }
  23. HStack {
  24. Text(landmark.park)
  25. Spacer()
  26. Text(landmark.state)
  27. }
  28. .font(.subheadline)
  29. .foregroundStyle(.secondary)
  30. Divider()
  31. Text("About \(landmark.name)")
  32. .font(.title2)
  33. Text(landmark.description ?? "")
  34. }
  35. .padding()
  36. Spacer()
  37. }
  38. .navigationBarTitle(landmark.name, displayMode: .inline)
  39. .navigationBarItems(trailing: FavoriteButton(isSet: landmarkIndex != nil ? $modelData.landmarkData[landmarkIndex!].isFavorite : .constant(false)))
  40. }
  41. }
  42. #Preview {
  43. let modelData = ModelData()
  44. return LandMarkDetail(landmark: modelData.landmarkData[0])
  45. .environment(modelData)
  46. }

每天学习一小步,进步一大步,Demo示例 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/780943
推荐阅读
相关标签
  

闽ICP备14008679号