代码库地址: https://gitee.com/dongzi1998/learn-ios

常用控件


  1. Text("Hello")
  2. .font(Front) // 设置字体
  3. .padding(Edge.Set,CGFloat) // 设置内边距
  4. .foregroundColor(Color) // 设置颜色
  5. .multilineTextAlignment()
  1. // name 是资源库中图片名称
  2. Image("Gopher")
  3. // 将此视图的尺寸限制为指定的纵横比。
  4. // .fit 填满空间
  5. .aspectRatio(contentMode: .fit)
  6. .clipShape(Circle()) // 设置剪辑形状为圆形
  7. .overlay{ // 对在此视图前面指定的视图进行分层
  8. Circle().stroke(.gray,lineWidth: 4) // 设置圆形弧线
  9. }
  10. .shadow(radius: 7) // 设置阴影
  1. struct Content_Previews: PreviewProvider {
  2. static var previews: some View {
  3. Content()
  4. .previewLayout(.fixed(width: 300, height: 70))
  5. }
  6. }
  1. struct Row: View {
  2. var text: String
  3. var body: some View{
  4. Text(self.text)
  5. }
  6. }
  7. struct List: View {
  8. var body: some View {
  9. List{
  10. Row(text: "row1")
  11. Row(text: "row2")
  12. Row(text: "row3")
  13. }
  14. }
  15. }
  1. List{
  2. ForEach(landmarks,id:\.id){ landmark in
  3. NavigationLink{
  4. LandmarkDetail(landmark: landmark)
  5. } label: {
  6. LandmarkRow(landmark: landmark)
  7. }
  8. }
  9. }
  1. struct Detail: View {
  2. var body: some View {
  3. Text("detail page")
  4. }
  5. }
  6. struct Demo: View {
  7. var body: some View {
  8. NavigationView {
  9. NavigationLink{
  10. Detail()
  11. }label: {
  12. Text("click me")
  13. }
  14. }
  15. }
  16. }
  1. struct Detail: View {
  2. var body: some View {
  3. Image("Gopher_1")
  4. }
  5. }
  6. struct Demo: View {
  7. @State private var showDetail = false
  8. var body: some View {
  9. VStack{
  10. if showDetail {
  11. Detail()
  12. }
  13. Spacer()
  14. HStack{
  15. Toggle("展示图片", isOn: $showDetail)
  16. }
  17. }
  18. }
  19. }
  1. extension AnyTransition {
  2. static var moveAndFade: AnyTransition {
  3. .asymmetric(
  4. insertion: .move(edge: .trailing).combined(with: .opacity),
  5. removal: .scale.combined(with: .opacity)
  6. )
  7. }
  8. }
  9. struct HikeView: View {
  10. var hike: Hike
  11. @State private var showDetail = false
  12. var body: some View {
  13. VStack {
  14. HStack {
  15. HikeGraph(hike: hike, path: \.elevation)
  16. .frame(width: 50, height: 30)
  17. VStack(alignment: .leading) {
  18. Text(hike.name)
  19. .font(.headline)
  20. Text(hike.distanceText)
  21. }
  22. Spacer()
  23. Button {
  24. withAnimation{
  25. showDetail.toggle()
  26. }
  27. } label: {
  28. Label("Graph", systemImage: "chevron.right.circle")
  29. .labelStyle(.iconOnly)
  30. .imageScale(.large)
  31. // 设置旋转效果
  32. .rotationEffect(.degrees(showDetail ? 90 : 0))
  33. // 设置比例效果
  34. .scaleEffect(showDetail ? 1.5 : 1)
  35. .padding()
  36. }
  37. }
  38. if showDetail {
  39. HikeDetail(hike: hike)
  40. // 设置自定义过渡效果(移动和褪色)
  41. .transition(.moveAndFade)
  42. }
  43. }
  44. }
  45. }
  1. withAnimation{
  2. // do something
  3. }
  4. Lable("")

布局


  1. // 以间隔器为轴,进行扩展
  2. // 可以实现CSS多列布局
  3. Spacer()
  1. Divider()
  1. // alignment: 设置对齐方式
  2. // .leading: 左对齐
  3. // .center: 居中对齐
  4. // .trailing: 右对齐
  5. // spacing: 间距
  6. VStask(alignment:.leading,spacing: 10) {
  7. Text("Text 1")
  8. Text("Text 2")
  9. }
  1. HStask {
  2. Text("Text 1")
  3. Text("Text 2")
  4. }
  1. // showsIndicators: 是否显示滚动条
  2. // .horizontal: 水平滚动
  3. // .vertical: 垂直滚动
  4. ScrollView(.horizontal,showsIndicators: false) {
  5. HStack {
  6. ForEach(1...4,id: \.self) { id in
  7. CheckOutCard()
  8. }
  9. }
  10. }
  1. struct ContentView: View {
  2. @State private var selection: Tab = .featured
  3. enum Tab {
  4. case featured
  5. case list
  6. }
  7. var body: some View {
  8. TabView(selection: $selection) {
  9. CategoryHome()
  10. .tag(Tab.featured)
  11. LandmarkList()
  12. .tag(Tab.list)
  13. }
  14. // 设置选项卡视图
  15. // .automatic 默认视图
  16. // .page 实现分页滚动TabView的TabViewStyle
  17. .tabViewStyle(.page)
  18. }
  19. }

资源


创建图像视图

Step1、点击项目目录中Assets

image.png

Step2、点击设置图片

image.png
image.png