gpt4 book ai didi

ios - SwiftUI 跨多个 subview 拖动手势

转载 作者:行者123 更新时间:2023-12-01 16:01:29 24 4
gpt4 key购买 nike

我正在尝试创建一个小方块 View 的网格,当用户用拇指悬停在它们上面(或在它们上滑动)时,小方块会暂时“弹出”并摇晃。然后,如果他们继续长按该 View ,则会打开另一个包含更多信息的 View 。

我认为在方形 View 上实现拖动手势就足够了,但看起来一次只有一个 View 可以捕获拖动手势。

有没有办法启用多个 View 来捕获拖动手势,或者一种为 iOS 实现“悬停”手势的方法?

这是我的主要网格 View :

import SwiftUI

struct ContentView: View {
@EnvironmentObject var data: PlayerData

var body: some View {
VStack {
HStack {
PlayerView(player: self.data.players[0])
PlayerView(player: self.data.players[1])
PlayerView(player: self.data.players[2])
}

HStack {
PlayerView(player: self.data.players[3])
PlayerView(player: self.data.players[4])
PlayerView(player: self.data.players[5])
}

HStack {
PlayerView(player: self.data.players[6])
PlayerView(player: self.data.players[7])
PlayerView(player: self.data.players[8])
}

HStack {
PlayerView(player: self.data.players[9])
PlayerView(player: self.data.players[10])
}
}
}
}

这是我的 Square View ,它将包含一个小摘要以显示在 Square 上:
import SwiftUI

struct PlayerView: View {
@State var scaleFactor: CGFloat = 1.0
var player: Player = Player(name: "Phile", color: .green, age: 42)

var body: some View {
ZStack(alignment: .topLeading) {
Rectangle().frame(width: 100, height: 100).foregroundColor(player.color).cornerRadius(15.0).scaleEffect(self.scaleFactor)

VStack {
Text(player.name)
Text("Age: \(player.age)")
}.padding([.top, .leading], 10)
}.gesture(DragGesture().onChanged { _ in
self.scaleFactor = 1.5
}.onEnded {_ in
self.scaleFactor = 1.0
})

}
}

最佳答案

这是可能的方法的演示......(它是您的应用程序数据设置的简化版本,但发展的想法和方向应该很清楚)

主要思想不是在项目 View 中而是在内容 View 中捕获拖动,在需要时(或如果)将需要的状态(或可计算的相关数据)传输到项目 View 中。

enter image description here

struct PlayerView: View {
var scaled: Bool = false
var player: Player = Player(name: "Phile", color: .green, age: 42)

var body: some View {
ZStack(alignment: .topLeading) {
Rectangle().frame(width: 100, height: 100).foregroundColor(player.color).cornerRadius(15.0).scaleEffect(scaled ? 1.5 : 1)

VStack {
Text(player.name)
Text("Age: \(player.age)")
}.padding([.top, .leading], 10)
}.zIndex(scaled ? 2 : 1)
}
}


struct ContentView: View {
@EnvironmentObject var data: PlayerData

@GestureState private var location: CGPoint = .zero
@State private var highlighted: Int? = nil

private var Content: some View {
VStack {
HStack {
ForEach(0..<3) { i in
PlayerView(scaled: self.highlighted == i, player: self.data.players[i])
.background(self.rectReader(index: i))
}
}
.zIndex((0..<3).contains(highlighted ?? -1) ? 2 : 1)

HStack {
ForEach(3..<6) { i in
PlayerView(scaled: self.highlighted == i, player: self.data.players[i])
.background(self.rectReader(index: i))
}
}
.zIndex((3..<6).contains(highlighted ?? -1) ? 2 : 1)
}
}

func rectReader(index: Int) -> some View {
return GeometryReader { (geometry) -> AnyView in
if geometry.frame(in: .global).contains(self.location) {
DispatchQueue.main.async {
self.highlighted = index
}
}
return AnyView(Rectangle().fill(Color.clear))
}
}

var body: some View {
Content
.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .global)
.updating($location) { (value, state, transaction) in
state = value.location
}.onEnded {_ in
self.highlighted = nil
})
}
}

关于ios - SwiftUI 跨多个 subview 拖动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59797968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com