【发布时间】:2020-06-14 10:02:44
【问题描述】:
我刚开始学习 SwiftUI,正在玩视图和动画,我想让控制器有点像苹果在控制中心的音量或亮度。但是我发现了一个有趣的小东西,我真的不知道该怎么办。
struct DragControler: View {
@GestureState var fillPercentGS : Double = 0.5
@State var fillPercent = 0.5
var body: some View {
ZStack{
Rectangle()
GeometryReader { geometry in
VStack(spacing: 0) {
Rectangle().foregroundColor(.red) //these rects
Rectangle().foregroundColor(.green).frame(height: geometry.size.height * CGFloat(self.fillPercent))
}.gesture(DragGesture(coordinateSpace: .local).updating(self.$fillPercentGS, body: { (value, state, transaction) in
state = Double(value.location.y/geometry.size.height)
}).onChanged({ (value) in
self.fillPercent = max(0, min(1 - Double(value.location.y/geometry.size.height), 1))
}))
}
Text("\(fillPercent)").foregroundColor(.white)
}.cornerRadius(50).aspectRatio(1/4, contentMode: .fit).padding()
}
}
GIF when one of rects fills entire view it becomes rounded
正如你在 gif 上看到的,当我将它一直拖到顶部或底部时,代码中明确表示为矩形的矩形变成圆角矩形
那么,为什么会这样呢?我试图用 clipShape(RoundedRect(...)) 替换 .cornerRadius() 修饰符,但它没有帮助。 但我发现将这些矩形的角半径显式设置为零会有所帮助。
Rectangle().cornerRadius(0).foregroundColor(.red)
Rectangle().cornerRadius(0).foregroundColor(...)
我猜这是 swiftUI 在后台做的一种优化
提前致谢
【问题讨论】: