1

我在 Mac 上的 SwiftUI 中的 MagnificationGesture 有问题。我正在编写一个 Mac 应用程序,我想缩放一个视图。当我运行该程序时,它可以正常运行几次,然后 onChanged 闭包不再执行。恐怕这是一个错误......(或者我完全误解了什么?)。实际上,我在 reddit 上发现了一个最近的问题,其中有人遇到了完全相同的问题:https ://www.reddit.com/r/SwiftUI/comments/sd43rk/im_having_an_issue_with_the_magnificationgesture/

我可以在一个非常简单的视图中重现该问题:

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
            .gesture(MagnificationGesture()
                        .onChanged({ value in
                print(value)
            }))
    }
}

我真的希望,有一个解决方案...

弗雷德里克 :)

4

1 回答 1

1

这是我稍微改编的代码——对我来说它工作正常,在 30 次之后也是如此(macOS 12.2beta,Xcode 13.2.1)

struct ContentView: View {
    
    @State private var scale: CGFloat = 1
    
    var body: some View {
        Text("Hello, world!")
            .scaleEffect(scale)
            .padding()
            .frame(width: 400, height: 400)
            .contentShape(Rectangle())
            .gesture(MagnificationGesture()
                        .onChanged({ value in
                scale = value
                print(value)
            }))
    }
}
于 2022-02-01T00:46:42.667 回答