如何创建可调整以适应其子视图之一扩展的SwiftUI HStack?

时间:2020-10-23 05:48:54

标签: ios swiftui gesture expand hstack

我有一个简单的水平堆栈,其中包含三个色条:

Like this:

当通过手势将黄色条的宽度放大时,我希望紫色条会向右移动以容纳展开的黄色条,但相反,黄色条只是在紫色条的下面展开(仍保持原始位置)

struct ContentView: View {
@GestureState var scale = CGFloat(1)

var body: some View {
    HStack(spacing: 5) {
        Color(.blue)
            .frame(width: 100, height: 60, alignment: .leading)
        Color(.yellow)
            .frame(width: 100, height: 60, alignment: .leading)
            .scaleEffect(x: scale, y: 1.0, anchor: .leading)

            .gesture(MagnificationGesture()
                .updating($scale, body: { (value, scale, trans) in
                    scale = value
                })

            )
        Color(.purple)
            .frame(width: 100, height: 60, alignment: .leading)
    }
}

}

如何在组件之一扩展的同时实现HStack布局的动态调整?

非常感谢...

罗伯特

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

Color(.yellow)
    .frame(width: CGFloat(100) * scale, height: 60, alignment: .leading)
    .gesture(MagnificationGesture()
                .updating($scale, body: { (value, scale, trans) in
                    scale = value
                })
    )