SwiftUI放大手势onUpdate不会立即在设备上触发(在模拟器上有效)

时间:2019-10-16 21:55:47

标签: swift swiftui

我的放大手势的onUpdate属性立即在模拟器上触发,但不在设备上触发

我尝试删除所有其他手势。似乎无关紧要。在iOS 13.1和13.2以及xcode 11.1和11.2上尝试过

import UIKit

struct ContentView: View {

    @State var gridDraggedX: CGFloat = 0
    @State var gridDraggedY: CGFloat = 0
    @State var accumulatedGridDraggedX: CGFloat = 0
    @State var accumulatedGridDraggedY: CGFloat = 0
    private var numCells: CGFloat = 10

    var dimension: CGFloat {
        numCells * cellSize
    }

    @State var initialCellSize: CGFloat = 50.0
    @State var cellSize: CGFloat = 50.0

    var body: some View {

        let dragGesture = DragGesture(minimumDistance: 5.0, coordinateSpace: .global)
            .onChanged({ value in
                self.gridDraggedX = value.translation.width + self.accumulatedGridDraggedX
                self.gridDraggedY = value.translation.height + self.accumulatedGridDraggedY
            }).onEnded({ value in
                self.gridDraggedX = value.translation.width + self.accumulatedGridDraggedX
                self.gridDraggedY = value.translation.height + self.accumulatedGridDraggedY
                self.accumulatedGridDraggedX = self.gridDraggedX
                self.accumulatedGridDraggedY = self.gridDraggedY
            })

        let dragGesture2 = DragGesture(minimumDistance: 0.0, coordinateSpace: .global)
            .onEnded({ value in
                if value.translation.width == 0 && value.translation.height == 0 {
                    print(value.startLocation)
                }
            })

        let magGesture = MagnificationGesture(minimumScaleDelta: 0.0).onChanged({ value in
                print(value)
                self.cellSize = self.initialCellSize * value
            }).onEnded({ value in
                self.cellSize = self.initialCellSize * value
                self.initialCellSize = self.cellSize
            })

        return ZStack(alignment: .topLeading) {
            GeometryReader { geometry in
                Path { path in
                    path.move(to: CGPoint(x:0, y: 0))
                    path.addLine(to: CGPoint(x: 0, y: self.dimension))
                    path.addLine(to: CGPoint(x: self.dimension, y: self.dimension))
                    path.addLine(to: CGPoint(x: self.dimension, y: 0))
                    path.addLine(to: CGPoint(x: 0, y: 0))
                }.fill(Color.red)
                Path { path in
                    for i in (0...Int(self.numCells)) {
                        path.move(to: CGPoint(x: 0 + CGFloat(i) * self.cellSize, y: 0))
                        path.addLine(to: CGPoint(x: 0 + CGFloat(i) * self.cellSize, y: self.dimension))
                    }
                    for i in (0...Int(self.numCells)) {
                        path.move(to: CGPoint(x: 0, y: 0 + CGFloat(i) * self.cellSize))
                        path.addLine(to: CGPoint(x: self.dimension, y: 0 + CGFloat(i) * self.cellSize))
                    }
                }.stroke(Color.blue)
                }.offset(x: gridDraggedX, y: gridDraggedY).simultaneousGesture(dragGesture).simultaneousGesture(dragGesture2).simultaneousGesture(magGesture)
        }
    }
}

我希望用户开始捏捏时,magGesture的onUpdate会正确触发。直到magGesture onUpdate马上触发。在使用

的模拟器上

编辑:因此在设备上似乎只是荒谬的。如果您的两个捏手指之一没有移动,则直到该值超过某个阈值时它才会触发onUpdate。 of

1 个答案:

答案 0 :(得分:0)

因此在设备上似乎只是荒谬的。如果您的两个捏手指之一没有移动,则直到该值超过某个阈值时它才会触发onUpdate。 oof-这个阈值似乎是任意的。也可能只是我的第二根手指移动,敲击某个速度或其他我不知道的时刻