SwiftUI放大显示手势



是否可以在视图上使用诸如放大手势之类的手势?我想这样做,这样我就可以一次缩放多个项目。

我写了下面的代码,但是这个手势从来没有被视图调用过。虽然它可以在许多其他对象上工作,如文本,图像,colorsquares .

struct TestView: View {
    @State var newScale: CGFloat = 1.0
    var body: some View {
        TestView1()
            .scaleEffect(newScale)
            .gesture (
                MagnificationGesture()
                    .onChanged { gesture in
                        self.newScale = gesture
                    }
            )
    }
    
}

谢谢你的帮助!

试试Apple文档中的这段代码。

struct MagnificationGestureView: View {
    @GestureState var magnifyBy = 1.0
    var magnification: some Gesture {
        MagnificationGesture()
            .updating($magnifyBy) { currentState, gestureState, transaction in
                gestureState = currentState
            }
    }
    var body: some View {
        TestViewOne()
            .frame(width: 100, height: 100)
            .scaleEffect(magnifyBy)
            .gesture(magnification)
    }
}

我认为.updating修饰语是这里的关键。文档:https://developer.apple.com/documentation/swiftui/gesture/updating(_:body:)/。同样,这段代码是对Apple Docs上的代码进行了轻微修改。链接在这里:https://developer.apple.com/documentation/swiftui/magnificationgesture.

最新更新