具有大量Button的VStack上的DragGesture,如何检测拖动何时在Button内部 [英] DragGesture on a VStack with lot of Buttons, how to detect when the drag is inside a Button

查看:76
本文介绍了具有大量Button的VStack上的DragGesture,如何检测拖动何时在Button内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UIKit中,可以使用以下代码完成此操作:

In UIKit this could be done with code like this:

if button.frame.contains(sender.location(in: rightStackView)) { ... }

,但是在SwiftUI中,我似乎找不到与frame.contains类似的任何东西, 那么如何确定拖动在特定按钮或其他视图中的时间?

but in SwiftUI I can't seem to find anything similar to frame.contains, so how can I find out when the drag is inside a specific button or other view?

推荐答案

好吧,这是很多代码,所以我尽可能简化了它,只是为了演示可能的方法(不带框架重叠,拖动重定位) ,浮动拖动项等).此外,从问题上不清楚将使用什么.无论如何,希望该演示会有所帮助.

Ok, it is a bit a lot of code, so I simplified it as much as possible just to demo possible approach (w/o frame overlapping, dragging relocation, floating drag item, etc.). Moreover it is not clear from the question for what it will be used. Anyway, hope this demo will be useful somehow.

注意:使用过Xcode 11.2

Note: used Xcode 11.2

这是结果

这是带有预览提供程序的一个模块演示代码

Here is one module demo code with Preview provider

import SwiftUI

struct DestinationDataKey: PreferenceKey {
    typealias Value = [DestinationData]

    static var defaultValue: [DestinationData] = []

    static func reduce(value: inout [DestinationData], nextValue: () -> [DestinationData]) {
        value.append(contentsOf: nextValue())
    }
}

struct DestinationData: Equatable {
    let destination: Int
    let frame: CGRect
}

struct DestinationDataSetter: View {
    let destination: Int

    var body: some View {
        GeometryReader { geometry in
            Rectangle()
                .fill(Color.clear)
                .preference(key: DestinationDataKey.self,
                            value: [DestinationData(destination: self.destination, frame: geometry.frame(in: .global))])
        }
    }
}

struct DestinationView: View {
    @Binding var active: Int
    let label: String
    let id: Int

    var body: some View {
        Button(action: {}, label: {
            Text(label).padding(10).background(self.active == id ? Color.red : Color.green)
        })
        .background(DestinationDataSetter(destination: id))
    }
}

struct TestDragging: View {
    @State var active = 0
    @State var destinations: [Int: CGRect] = [:]

    var body: some View {
        VStack {
            Text("Drag From Here").padding().background(Color.yellow)
                .gesture(DragGesture(minimumDistance: 0.1, coordinateSpace: .global)
                    .onChanged { value in
                        self.active = 0
                        for (id, frame) in self.destinations {
                            if frame.contains(value.location) {
                                self.active = id
                            }
                        }
                    }
                    .onEnded { value in
                        // do something on drop
                        self.active = 0
                    }
            )
            Divider()
            DestinationView(active: $active, label: "Drag Over Me", id: 1)
        }.onPreferenceChange(DestinationDataKey.self) { preferences in
            for p in preferences {
                self.destinations[p.destination] = p.frame
            }
        }
    }
}

struct TestDragging_Previews: PreviewProvider {
    static var previews: some View {
        TestDragging()
    }
}

这篇关于具有大量Button的VStack上的DragGesture,如何检测拖动何时在Button内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆