首页 > 解决方案 > SwiftUI Magnification gesture onUpdate doesn't fire immediately on device (works on simulator)

问题描述

the onUpdate property of my magnification gesture fires immediately on the simulator, but not on device

I have tried removing all other gestures. Does not seem to matter. Tried on iOS 13.1 and 13.2 and xcode 11.1 and 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)
        }
    }
}

I expect the onUpdate of the magGesture to fire right when the user starts to pinch. It doesn't fire until magGesture onUpdate right away. On a simulator using

EDIT: so on the device it seems to be just hella finicky. if one of your two pinching fingers doesn't move then it doesn't fire onUpdate until the value is above a certain threshold. oof

标签: swiftswiftui

解决方案


所以在设备上它似乎只是很挑剔。如果您的两个捏手指中的一个没有移动,那么它不会触发 onUpdate 直到该值高于某个阈值。oof - 这个阈值似乎是任意的。也可能是我的第二根手指移动的那一刻,某个速度被击中,或者其他我不知道的东西


推荐阅读