无法将 swiftui 视图动态调整为新的运行时定义的尺寸

Cannot dynamically resize swiftui view to new runtime defined dimensions

我正在编写一个带有 RectangleView 的视图,我希望每次点击 RectangleView 本身时缩小一个百分比(比如说当前大小的 0.8%)。

这里是一个视图示例:

struct MainView: View {
    
    @State private var buttonHeight: CGFloat = .infinity
    @State private var buttonWidth: CGFloat = .infinity
    
    
    var body: some View {
        VStack{
            Text("Reduce")
            GeometryReader{ geometry in
                VStack() {  // added the Vstack to center the Rectangle within the GeometryReader (known bug)  
                    RoundedRectangle(cornerRadius: 25.0).foregroundColor(.purple).frame(
                        width: $buttonWidth.wrappedValue, height: $buttonHeight.wrappedValue, alignment: .center).padding(.all)
                        .onTapGesture {
                            let currWidth = geometry.size.width
                            let currHeight = geometry.size.height
                            print(">> geom width \(currWidth)")
                            print(">> geom height \(currHeight)")
                            buttonWidth = currWidth * 0.8 // CGFloat(currWidth * 0.8)
                            buttonHeight = currHeight * 0.8// CGFloat(currHeight * 0.8)
                            print(">> buttonWidth \(buttonWidth)")
                            print(">> buttonHeight \(buttonHeight)")
                            print(">> buttonwidthwrapped \($buttonWidth.wrappedValue)")
                    
                        }
                        
                }.frame(width: geometry.size.width,
                        height: geometry.size.height,
                        alignment: .center)
            }
        }
    }
}

好像是第一次点击矩形时我调整了它的大小,但是当我再次点击时它就不会再发生了。

如何在不让 swiftui 重新计算视图尺寸的情况下强制框架的特定值?

初始值只需要GeometryReader。对于其余的水龙头,只需使用变量中的内容即可。

您可以通过将 reader 置于后台来执行此操作。

import SwiftUI
struct ScalingRectangle: View{
    @State private var buttonHeight: CGFloat = .infinity
    
    @State private var buttonWidth: CGFloat = .infinity
    var body: some View {
        VStack{
            Text("Reduce")
            
            RoundedRectangle(cornerRadius: 25.0)
                .foregroundColor(.purple)
            //re-size with initial value by the background and then onTap
                .frame(width: buttonWidth, height: buttonHeight, alignment: .center)
                .onTapGesture {
                    //Update values on tap
                    buttonWidth = buttonWidth * 0.8
                    buttonHeight = buttonHeight * 0.8
                }
                .background(
                    GeometryReader{ geo in
                        Color.clear
                            .onAppear(perform: {
                                //Get the initial value
                                self.buttonHeight = geo.size.height
                                self.buttonWidth = geo.size.width
                            })
                    }
                )
            
        }
    }
}
struct ScalingRectangle_Previews: PreviewProvider {
    static var previews: some View {
        ScalingRectangle()
    }
}