xcode 同时长按和拖动手势SwiftUI

vulvrdjw  于 2023-03-24  发布在  Swift
关注(0)|答案(1)|浏览(157)

我有一个带摄像头的应用程序,有一个捕获按钮,我想长按屏幕上的记录和自由移动它,并能够放大和缩小。现在我不担心记录和缩放部分,我没有做的是处理两个手势来移动按钮和缩放按钮时,它的记录。

e5nszbig

e5nszbig1#

iOS 16.0你要的我已经试过了,这里有一个示例代码,有一个观点,长按会放大,然后你可以自由移动。

enum DragState {
    case inactive
    case pressing
    case dragging(translation: CGSize)
    
    var translation: CGSize {
        switch self {
        case .inactive, .pressing:
            return .zero
        case .dragging(let translation):
            return translation
        }
    }
    
    var isDragging: Bool {
        switch self {
        case .dragging:
            return true
        case .pressing, .inactive:
            return false
        }
    }
    
    var isPressing: Bool {
        switch self {
        case .inactive:
            return false
        case .pressing, .dragging:
            return true
        }
    }
}

struct ContentView: View {
    //MARK: - PROPERTIES
    @GestureState private var dragState = DragState.inactive
    //MARK: - BODY
    var body: some View {
        VStack {
            Text("Press and Drag")
                .padding(20)
                .background(Color.blue)
                .foregroundColor(Color.white)
                .offset(x: self.dragState.translation.width, y: self.dragState.translation.height)
                .scaleEffect(dragState.isDragging ? 1.85 : 1)
                .animation(.interpolatingSpring(stiffness: 120, damping: 120), value: 1)
                .gesture(LongPressGesture(minimumDuration: 0.01)
                    .sequenced(before: DragGesture())
                    .updating(self.$dragState, body: { (value, dstate, transaction) in
                        switch value {
                        case .first:
                            dstate = .pressing
                        case .second(true, let drag):
                            dstate = .dragging(translation: drag?.translation ?? .zero)
                        default:
                            break
                        }
                    })
                )
        }
    }
}

按住拖动前

按住拖动时

相关问题