当前位置: 首页 > news >正文

SwiftUI 常用组件和属性(SwiftUI初学笔记)

本文为初学SwiftUI笔记。记录SwiftUI常用的组件和属性。

组件

共有属性(View的属性)

             Image("toRight").resizable().background(.red) // 背景色.shadow(color: .black, radius: 2, x: 9, y: 15) //阴影.frame(width: 30, height: 30) // 宽高 可以只设置宽或者高.offset(x: 10,y: 10) //显示位置偏移(原来的位置可能还继续占位).position(CGPoint(x: 175, y: 100)) //绝对位置 这个配置的是中心点的位置
//            //相对于其他组件的间距.padding(EdgeInsets(top: 10, leading: 50, bottom: 50, trailing: 50)).scenePadding(.trailing) // 填充此视图所沿的一组边。.border(.blue, width: 12) // 边框颜色和宽度.cornerRadius(15) // 圆角.clipShape(Circle()) //裁减成圆形.mask(Circle()) //遮罩.onTapGesture { // 点击事件print("你好啊")}

Text

Text展示文案

            Image("toRight").resizable().background(.red) // 背景色.shadow(color: .black, radius: 2, x: 9, y: 15) //阴影.frame(width: 30, height: 30) // 宽高 可以只设置宽或者高.offset(x: 10,y: 10) //显示位置偏移(原来的位置可能还继续占位)
//            //相对于其他组件的间距.padding(EdgeInsets(top: 10, leading: 50, bottom: 50, trailing: 50)).scenePadding(.trailing) // 填充此视图所沿的一组边。.border(.blue, width: 12) // 边框颜色和宽度.cornerRadius(15) // 圆角.overlay(Color.blue.clipShape(Circle()))//覆盖一个图形.rotation3DEffect(.degrees(60), axis: (x: 1, y: 0, z: 0))//3D旋转.rotationEffect(.degrees(20), anchor:UnitPoint(x: 0, y: 0)) //2D旋转.onTapGesture { // 点击事件print("你好啊")}

Image

Image展示图片

            Image("toRight") //如果不设置大小会自动读取图片大小//允许重新设置大小,调用了这个才能设置大小 ,capInsets为间距 resizingMode为渲染模式.resizable(capInsets: EdgeInsets(), resizingMode: .tile).interpolation(.high)//插值,大图片用高插值,优化展示.antialiased(true) //抗锯齿

ProgressView

ProgressView是进度条

import SwiftUIstruct ProgressTest: View {@State var timer = Timer.TimerPublisher(interval: 0.03, runLoop: .main, mode: .common).autoconnect()@State var value:Double = 0.0var body: some View {List{//菊花 无法定义颜色 ProgressView()//线性进度条 无法隐藏 Label ProgressView("完成量", value: value, total: 100).accentColor(.red)//自定义 StyleProgressView("工程进度",value: value, total: 100).progressViewStyle(MyProgressViewStyle())}.onAppear {timer = Timer.TimerPublisher(interval: 0.03, runLoop: .main, mode: .common).autoconnect()}.onReceive(timer) { _ inif value < 100 {value += 2}}}
}//定义方法都大同小异。
struct MyProgressViewStyle:ProgressViewStyle{let foregroundColor:Colorlet backgroundColor:Colorinit(foregroundColor:Color = .blue,backgroundColor:Color = .orange){self.foregroundColor = foregroundColorself.backgroundColor = backgroundColor}func makeBody(configuration: Configuration) -> some View {GeometryReader{ proxy inZStack(alignment:.topLeading){backgroundColorRectangle().fill(foregroundColor).frame(width:proxy.size.width * CGFloat(configuration.fractionCompleted ?? 0.0))}.clipShape(RoundedRectangle(cornerRadius: 10)).overlay(configuration.label.foregroundColor(.white))}}
}

Button

按钮

Button(action: { //点击事件}, label: { //这个里面放的东西相当于水平布局Image(systemName: "clock")Text("Click Me")Text("Subtitle")
})

如果只有文字可以直接这样

Button("Click Me") {// 点击事件
}

布局组件

VStack

垂直布局
alignment是对齐方式 包括leading、trailing、center
spacing 是内部组件间距

VStack(alignment:.leading, spacing: 20){
}

HStack

水平布局
alignment是对齐方式 包括 包括bottom、top、firstTextBaseline、lastTextBaseline、center
spacing 是内部组件间距

    HStack(alignment:.bottom, spacing: 20){}

ZStack

堆叠布局,后面的组件叠在前面的组件之上
alignment 包括leading、trailing、bottom、top、bottomLeading、topLeading、bottomtrailing、toptrailing、center

//ZStack(alignment: .bottomTrailing){}

Group

把多个组件组合到一起,解决布局最多只能有10个内部组件问题,他里面的组件遵循外面的布局的布局方式。另外还有个组件GroupBox,是带一个文案的Group。

LazyStack

懒加载的Stack,需要跟scrollView配合使用,只会加载当前页面显示的item,必须有循环创建,否则无法出现懒加载效果

颜色

使用系统提供色

Text().background(.yellow)

自定义颜色

Text().background(Color(UIColor(displayP3Red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))

使用Asset中定义的颜色,下面的8FD3F4是命名为此的asset中的颜色

Color("8FD3F4")

渐变色

LinearGradient(gradient: Gradient(colors: [Color.blue, Color.green]), startPoint: .leading, endPoint: .trailing)

形状

在这里插入图片描述

如上形状的代码:

        VStack {Spacer()Group{Text("矩形 Rectangle")Rectangle().fill(Color.orange).frame(width: 100, height: 100)}Group{Text("圆角矩形 RoundedRectangle")RoundedRectangle(cornerRadius: 25, style: .continuous).fill(Color.red).frame(width: 100, height: 100)}Group{Text("胶囊 Capsule")Capsule().fill(Color.green).frame(width: 100, height: 50)Text("椭圆形 Ellipse")Ellipse().fill(Color.blue).frame(width: 100, height: 50)}Group{Text("圆形 Circle")Circle().fill(Color.purple).frame(width: 100, height: 50)}Spacer()}

其他

在 UIKit 中使用 SwiftUI

let vc = UIHostingController(rootView: Text("Hello World")) // 包装出来一个 UIViewController
let view = vc.view // 可以获取 view
http://www.lryc.cn/news/36368.html

相关文章:

  • Centos 中设置代理的两种方法
  • 高速PCB设计指南系列(一)
  • 云端IDE:TitanIDE v2.6.0 正式发布
  • 【Python】tqdm 模块
  • 论文阅读:Adversarial Cross-Modal Retrieval对抗式跨模式检索
  • 计算机网络复习
  • unity动画--动画绑定,转换,用脚本触发
  • 车载汽车充气泵PCBA方案
  • Android 连接 MySQL 数据库教程
  • tmall.item.update.schema.get( 天猫编辑商品规则获取 )
  • Leetcode 2379. 得到 K 个黑块的最少涂色次数
  • [深入理解SSD系列 闪存实战2.1.3] 固态硬盘闪存的物理学原理_NAND Flash 的读、写、擦工作原理
  • 总结:Linux内核相关
  • flutter工程创建过程中遇到一些问题。
  • 记录实现操作系统互斥锁的一次思考
  • 计算机SCI期刊的分值是什么意思? - 易智编译EaseEditing
  • 5MW风电永磁直驱发电机-1200V直流并网MATLAB仿真模型
  • 10 个常见的 JavaScript 面试问题以及如何回答它们
  • 字节跳动-今日头条后端开发一面面经
  • 再见 ETHDenver 2023
  • 阿里云dataworks表操作
  • 【latex】总结最近使用到的画图、表格及公式操作
  • excel表格数字乱码怎么恢复正常
  • 泰山众筹电商模式的分析
  • [算法]归并排序
  • 【UE4 RTS游戏】05-自定义日期和时间
  • ES的restful风格的HTTP方法详解
  • 第十三章 opengl之模型(导入3D模型)
  • html标签表示!
  • 前端优化,webpack打包删除无用文件,并附上批量删除文件脚本!非常好用