【问题标题】:Prevent MagnificationGesture zooming below original size of image防止 MagnificationGesture 缩放到图像的原始大小以下
【发布时间】:2023-01-14 08:15:20
【问题描述】:

我当前对 MagnificationGesture 的实现允许将图像压缩并缩小到非常小的尺寸。我只想允许放大图像,因此尝试实现 minZoom 变量来尝试控制手势是否允许用户缩小。绝不允许图像缩小到超过其原始尺寸。

我已经开始使用这段代码,但它无法正常工作,有人可以帮忙解决吗?

let minZoom: CGFloat = 1

var images: [Space.SpaceImage]

@GestureState var scale: CGFloat = 1
@State private var imageScale: CGFloat = 1.0

var magnification: some Gesture {
    MagnificationGesture()
        .updating($scale) { currentState, gestureState, _ in
            gestureState = currentState
            if currentState >= minZoom {
                imageScale = scale
            }
        }
}

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    可以在设置scaleEffect时使用max函数来确保它永远不会小于minZoom,如下:

    struct ContentView: View {
        
        let minZoom: CGFloat = 1
        @State var scale: CGFloat = 1
        
        var body: some View {
            Image(systemName: "questionmark")
                .resizable()
                .frame(width: 100, height: 100)
                .scaleEffect(x: max(scale, minZoom), y: max(scale, minZoom))
                .gesture(magnification)
        }
        
        var magnification: some Gesture {
            MagnificationGesture()
                .onChanged { scale in
                    self.scale = scale
                }
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 2020-03-19
      • 2017-06-10
      • 1970-01-01
      • 2015-02-14
      相关资源
      最近更新 更多