0

我正在尝试使用scaleEffectand水平缩放视图MagnificationGesture。它几乎可以按我的意愿工作,ScrollView只是在其子视图调整大小时不会调整大小。

有解决办法吗?任何解决方案将不胜感激。

重现:

  1. 运行下面的代码
  2. 使用捏合手势水平放大图像
  3. 注意ScrollView滚动,好像图像仍然具有相同的大小。
struct ContentView: View {

    @State private var currentAmount: CGFloat = 0
    @State private var finalAmount: CGFloat = 1

    var body: some View {
        ScrollView(.horizontal) {
            Image(systemName: "star")
                .resizable()
                .scaledToFit()
                .scaleEffect(x: finalAmount + currentAmount, y: 1)
                .gesture(
                    MagnificationGesture()
                        .onChanged { amount in
                            self.currentAmount = amount - 1
                        }
                        .onEnded { amount in
                            self.finalAmount += self.currentAmount
                            self.currentAmount = 0
                        }
                )
        }
        .frame(maxHeight: 300)
    }
}
4

1 回答 1

0

这个观点做到了。

struct HorizontalScaleView<Content: View>: View {

    @ViewBuilder var content: Content

    @State private var currentAmount: CGFloat = 0
    @State private var finalAmount: CGFloat = 1

    var body: some View {
        GeometryReader { geo in
            ScrollView(.horizontal) {
                content
                    .scaledToFit()
                    .scaleEffect(x: finalAmount + currentAmount, y: 1)
                    .frame(width: (finalAmount + currentAmount) * geo.size.width, height: geo.size.width)
                    .gesture(
                        MagnificationGesture()
                            .onChanged { amount in
                                self.currentAmount = amount - 1
                            }
                            .onEnded { _ in
                                if self.finalAmount + self.currentAmount >= 1 {
                                    self.finalAmount += self.currentAmount
                                } else {
                                    self.finalAmount = 1
                                }
                                self.currentAmount = 0
                            }
                    )
            }
        }
    }
}
于 2021-10-03T08:16:36.390 回答