swift - 开发基于屏幕大小的 SwiftUI 算法
问题描述
所以我有一个滑块,我有一个小的 VStack 框架,里面有一些文本,当你触摸滑块时我想移动它们(我希望 VStack 一直保持在滑块的正上方)。我不确定是否有更好的方法来做到这一点,所以我尝试通过使其依赖于滑块的值来调整 VStack 的前导填充。这是我的代码:
struct ViewName: View {
@State private var sliderValue: Double = 0.0
var body: some View {
VStack {
HStack {
VStack {
Text("t")
}.frame(height: 30).background(Color.white)
.padding(.leading, CGFloat(CGFloat(DayRangeValue) * (UIScreen.main.bounds.width * 0.7)/24) + ((UIScreen.main.bounds.width * 0.15) + 10))
//The above padding is the algorithm: the addition of (UIScreen.main.bounds.width * 0.15 + 10) essentially works; it tells the VStack the starting position, and works on diff device simulators.
Spacer()
}
Slider(value: $sliderValue, in: 0...23).frame(width: UIScreen.main.bounds.width * 0.7)
//Note: the 0...23 cannot be changed, and the width of the slider is a percentage of screen size because I figured it can't be some static value, as that would only work on one device
}
}
}
长话短说,我希望“t”在滑块上方,无论它在哪里;在任何设备上。上述填充算法目前在其中一款 iPad 模拟器上运行良好,但在 iPhone 8 模拟器上无法正常运行(例如)。如果有人可以帮助我找出适用于所有设备的基于屏幕尺寸的填充算法,那就太棒了。
解决方案
如果我正确理解了您的意图,这是可能的解决方案(与几何无关)。使用 Xcode 11.4 / iOS 13.4 测试
let kKnobWidth = CGFloat(24)
struct DemoLabelAboveSlider: View {
var range: ClosedRange<Double> = 0...23
@State private var sliderValue: Double = 0.0
var body: some View {
VStack(alignment: .leading) {
Slider(value: self.$sliderValue, in: self.range)
.padding(.vertical)
.overlay(GeometryReader { gp in
Text("t")
.position(x: CGFloat(self.sliderValue) * (gp.size.width - kKnobWidth) / CGFloat(self.range.upperBound) + kKnobWidth / 2, y: 0)
})
}
.padding(.horizontal) // << this one is only for better demo
}
}
推荐阅读
- rxjs - Angular 6 - Form.valueChanges 不更新 form.touched 属性(模板驱动的表单)
- android - 聚合来自多个模块的 gradle 测试结果
- terraform - 如何让 Terraform 中的 Docker 提供程序在尝试连接之前等待地址可用?
- docker - .net 3.5 wcf 应用程序使用哪个 windows docker 基础映像
- java - 我如何将 odoo 登录 api 用于 postman 和 java 等外部应用程序?
- html - 滚动导航栏时更改粘性菜单的字体颜色?
- mysql - 如何在 Go 中创建 MySQL 数据库(表)并执行 CRUD 操作
- function - 打印给定 xy 维度的网格坐标列表
- java-stream - 如何使用 Streams 将地图转换为地图的地图,即嵌套地图
- electron - Electron如何拦截http响应体