๐SlidersForSize
Last updated
Last updated
// 2022.02.15 (*)
// 2022.02.17 (+) struct: SliderWithSubtitle
// (f) bug : step can be < 1
import SwiftUI
// โโโโโโโโโโโโโโโโโโโโโโโโ
// โ SlidersForSize โ
// โโโโโโโโโโโโโโโโโโโโโโโโ
/// ๐ sliders to control width/height
/// ```
/// SlidersForSize($size)
/// ```
struct SlidersForSize: View {
// โญ๏ธ Binding variable
@Binding var size: CGSize
var body: some View {
VStack(spacing: 20) {
// ๐ Slider: control offered width
SliderWithSubtitle(
value : $size.width,
subtitle: "width"
)
// ๐ Slider: control offered height
SliderWithSubtitle(
value : $size.height,
range : 100...300,
subtitle: "height",
tint : .blue
)
}
}
}
// convenience init
extension SlidersForSize {
/// convenience init.
/// ```
/// SlidersForSize($size)
/// ```
init(_ size: Binding<CGSize>) {
// โญ๏ธ ๆณจๆ๏ผไธๆฏ `self.size = size` โ๏ธโ๏ธโ๏ธ
self._size = size
}
}
// โโโโโโโโโโโโโโโโโโ
// โ Previews โ
// โโโโโโโโโโโโโโโโโโ
struct SlidersForSize_Previews: PreviewProvider {
static var previews: some View {
SlidersForSize(size: .constant(.init(300,200)))
}
}
// 2022.02.17 (+) struct: SliderWithSubtitle
import SwiftUI
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// โ SliderWithSubtitle โ
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
/// slider with subtitle below it.
/// ```
/// SliderWithSubtitle(
/// value : $size.width,
/// subtitle: "width"
/// )
///
/// // with all parameters
/// SliderWithSubtitle(
/// value : $size.height,
/// range : 100...300,
/// subtitle: "height",
/// tint : .blue
/// )
struct SliderWithSubtitle: View {
// โญ๏ธ Binding variable
@Binding var value: CGFloat
var range: ClosedRange<CGFloat> = 20...500
var step: CGFloat = 1
let subtitle: String
var decimalPlaces = 0
var tint: Color = .pink
var body: some View {
VStack {
// ๐ `Slider` to control the `value`
Slider(value: $value, in: range, step: step)
.tint(tint)
// subtitle for slider
Text("\(subtitle): \(value.decimalPlaces(decimalPlaces))")
.font(.system(.caption, design: .monospaced))
.foregroundColor(.secondary)
}
}
}
used in MyGrid.
used in demo view showing the problem with .readSize().
2022.02.17
// 2022.02.15
import SwiftUI
/// ๐ sliders to control width/height
struct SlidersForSize: View {
// โญ๏ธ Binding variable
@Binding var size: CGSize
var body: some View {
VStack(spacing: 20) {
// ๐ `Slider` to control the offered width
Slider(value: $size.width, in: 20...500, step: 1)
// label for offered width
Text("offered width: \(Int(size.width))")
.font(.system(.caption, design: .monospaced))
.foregroundColor(.secondary)
// ๐ `Slider` to control the offered width
Slider(value: $size.height, in: 20...500, step: 1)
.tint(.blue)
// label for offered width
Text("offered height: \(Int(size.height))")
.font(.system(.caption, design: .monospaced))
.foregroundColor(.secondary)
}
.frame(width: 300)
.padding()
}
}
extension SlidersForSize {
/// convenience init.
/// ```
/// SlidersForSize($size)
/// ```
init(_ size: Binding<CGSize>) {
// โญ๏ธ ๆณจๆ๏ผไธๆฏ `self.size = size` โ๏ธโ๏ธโ๏ธ
self._size = size
}
}
struct SlidersForSize_Previews: PreviewProvider {
static var previews: some View {
SlidersForSize(size: .constant(.init(300,200)))
}
}