实战问题
我有两个 WheelPicker 包含在 HStack 中,用于“小时”和“分钟”。每个 Picker 都设置在一个框架内(宽度:50,高度:30)并额外裁剪。
在 iOS14 中,它的行为符合预期,我可以滚动“小时”选择器来更改小时和“分钟”选择器来更改分钟。
然而,在 iOS15 中,“分钟”轮选择器扩展到超过 50 帧宽度并重叠到“小时”选择器中;如果我在“小时”选择器上滚动,“分钟”值会发生变化(而不是“小时”值),如果我在“分钟”选择器上滚动,它会按预期更改“分钟”。如果我触摸“小时”选择器外的最左侧,则“小时”值会发生变化。
任何人都有相同的问题以及此问题的任何解决方法?
我遇到了一种添加“掩码(矩形()”的解决方法并尝试了它,但它在 iOS15 上不起作用。
@State private var hour: Int = 0
@State private var minute: Int = 0
var body: some View {
VStack {
HStack (alignment: .center, spacing: 3) {
NumberPicker("", selection: $hour
, startValue: 0
, endValue: 23
, pickerSize: CGSize(width: 50, height: 30)
)
Text("hr")
NumberPicker("", selection: $minute
, startValue: 0
, endValue: 59
, pickerSize: CGSize(width: 50, height: 30)
)
Text("min")
} // HStack
} // VStack
}
}
struct NumberPicker: View {
let startValue: Int
let endValue: Int
let pickerSize: CGSize
let title: String
@Binding var selection: Int
@State var value: Int = 0
init(_ title: String = ""
, selection: Binding<Int>
, startValue: Int = 0
, endValue: Int
, pickerSize: CGSize = CGSize(width: 50, height: 30)
) {
self._selection = selection
self.title = title
self.startValue = startValue
self.endValue = (endValue + 1)
self.pickerSize = pickerSize
self._value = State(initialValue: selection.wrappedValue)
}
var body: some View {
Picker(title, selection: $value) {
ForEach(startValue..<endValue, id: \.self) { currentValue in
Text("\(currentValue)")
.tag(currentValue)
}
}
.pickerStyle(WheelPickerStyle())
.fixedSize(horizontal: true, vertical: true)
.frame(width: pickerSize.width, height: pickerSize.height)
.clipped(antialiased: true)
}
}
解决方案
在NumberPicker尝试添加compositingGroup之前clipped(…):
.compositingGroup()
.clipped(antialiased: true)
精品教程推荐
加入我们一起学习SwiftUI
QQ:3365059189
SwiftUI技术交流QQ群:518696470
教程网站:www.openswiftui.com