小编ele*_*ert的帖子

SwiftUI - 拖放圆圈

我正在尝试创建可以拖放的圆圈。

我可以让它仅与第一个圆一起工作,但是,第一个圆之后的任何东西都不起作用。

预期行为:拖动时圆圈跟随我的光标,并在拖动结束时落在最终位置

实际行为:圆圈跟随光标的水平位置,但不跟随垂直位置(垂直位置始终明显低于光标)

ContentView.swift

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(alignment: .center) {
            ForEach(0..<5) { _ in
                DraggableCircles()
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct DraggableCircles: View {
    @State var dragAmount: CGPoint = CGPoint.zero

        var body: some View {
            Circle().fill(Color.red)
            .frame(width: 50, height: 50)
                .gesture(
                    DragGesture(coordinateSpace: .global).onChanged {action in
                        let location = action.location
                        let newWidth = location.x
                        let newHeight = location.y
                        let size …
Run Code Online (Sandbox Code Playgroud)

user-interface drag-and-drop ios swift swiftui

3
推荐指数
1
解决办法
1292
查看次数

标签 统计

drag-and-drop ×1

ios ×1

swift ×1

swiftui ×1

user-interface ×1