SwiftUI 删除特定区域中的视图拖放

SwiftUI Delete a View drag and drop in specific area

提问人:Gonzalo M. Rivas 提问时间:11/6/2023 更新时间:11/6/2023 访问量:46

问:

我正在尝试将contextMenu添加到此视图中,但是由于它是可拖动的,因此我认为当它被拖出框架时,当显示contextMenu时无法将图像显示到视图中...我可以看到 2 种可能的解决方案,但我不能做得很好...... 1- 当您调用 contextMenu 时,图像位于框架的中心...

2-拖放到特定区域以将其删除...

有什么想法吗?你能帮我吗?:)

这里的代码...

 import SwiftUI

struct PiercingView: View {
    var piercing: Piercing
    @State private var position = CGPoint(x: 100, y: 100)
    @ObservedObject var piercings: PiercingModel = .init()
    @State private var frame: CGFloat = 100

    @State private var magnify: CGFloat = 1
    @State private var lastMagnify: CGFloat = 1
    @State private var angle: Angle = .degrees(.zero)
    @State private var lastAngle: Angle = .degrees(.zero)
   
    var body: some View {
        VStack {
            
            Image(piercing.image)
                .resizable()
                
                .aspectRatio(contentMode: .fit)
                .position(position)
                
                .gesture(
                    DragGesture()
                        .onChanged { value in
                            position = value.location
                        }
                )
                .scaleEffect(self.magnify)
                .rotationEffect(self.angle)
                .gesture(RotationGesture()
                    .onChanged { angle in
                        self.angle = angle + self.lastAngle
                    }
                    .onEnded { _ in
                        self.lastAngle = self.angle
                    }
                )
                .simultaneousGesture(MagnificationGesture()
                    .onChanged { v in
                        let delta = v / self.lastMagnify
                        self.lastMagnify = v
                        self.magnify *= delta
                    }
                    .onEnded { _ in
                        self.lastMagnify = 1
                    }
                )
                .contextMenu(menuItems: {
                    Button(role: .destructive) {
                        piercings.deletePiercing(piercing: piercing)
                    }
                label: {
                    HStack {
                        Text("Delete")
                        Image(systemName: "trash")
                    }
                }
                })
              
        }.frame(width: frame, height: frame)
          
    }
}

Here when I call contextMenu in the View

This is the other possible solution

谢谢!

Swift SwiftUI 视图 拖放 上下文菜单

评论


答: 暂无答案