简体   繁体   English

如何使用 onTapGesture 更改视图的偏移量?

[英]How to change the offset of an view using onTapGesture?

I am trying to move a view when it is being tapped.我试图在被点击时移动视图。 But unfortunately nothing happens.但不幸的是,什么也没发生。 How do I toggle the position of it using the offset y axis?如何使用偏移 y 轴切换它的 position?

    struct Test: View {
    @State var cards : [Card] = [Card(), Card(), Card(), Card(), Card(), Card()]

    var body: some View {
        VStack {
            ZStack (alignment: .top){
                ForEach (0..<cards.count) { i in
                    RoundedRectangle(cornerRadius: 10).frame(width: 250, height: 150)
                        .foregroundColor(Color.random).offset(y: self.cards[i].isFocus ? CGFloat(500) : CGFloat(i*50))
                        .zIndex(Double(i))
                        .onTapGesture {
                            self.cards[i].isFocus.toggle() // now the touched Card should move to an offset of y: 500, but nothing happens
                        }
                }
            }
            Spacer()
        }


    }
}

struct Card :Identifiable{
    @State var isFocus :Bool = false
    var id  = UUID()

}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test()
    }
}

extension Color {
    static var random: Color {
        return Color(red: .random(in: 0...1),
                     green: .random(in: 0...1),
                     blue: .random(in: 0...1))
    }
}

I see your code and I am sorry to say but its wrong in many aspects.我看到了您的代码,很抱歉,但它在许多方面都是错误的。 I would recommend you to go through SwiftUI basics and understand how and when we use @State, @ObservedObject, @Binding etc. Also always try to break your Views into the smallest components.我会推荐你从 go 到 SwiftUI 基础知识,并了解我们如何以及何时使用@State、@ObservedObject、@Binding 等。也总是尝试将你的视图分解成最小的组件。 Below is the code which I think fulfills your requirement.以下是我认为满足您要求的代码。

struct Test: View {
    var cards : [Card] = [Card(), Card(), Card(), Card(), Card(), Card()]

    var body: some View {

        VStack {
            ZStack (alignment: .top){
                ForEach (0..<cards.count) { i in
                    CardView(card: self.cards[i], i: i)
                }
            }
            Spacer()
        }
    }
}

struct CardView: View {

    @ObservedObject var card: Card
    let i: Int

    var body: some View {

       RoundedRectangle(cornerRadius: 10).frame(width: 250, height: 150)
            .foregroundColor(Color.random)
            .offset(y: card.isFocus ? CGFloat(500) : CGFloat(i*10))
            .zIndex(Double(i))
            .onTapGesture {
                withAnimation {
                  self.card.isFocus.toggle()
                }
        }
    }
}

class Card: ObservableObject{
    @Published var isFocus: Bool = false
    var id  = UUID()

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 SwiftUI 中列表的每个元素的 onTapGesture 切换到另一个视图? - How to switch to another view by each element's onTapGesture of a list in SwiftUI? 如何在 SwiftUI 文本视图中为特定单词添加 onTapGesture 和下划线? - How to add onTapGesture AND underline to specific word in a SwiftUI Text view? onTapGesture 后偏移不会重置 - SwiftUI - Offset doesn't reset after onTapGesture - SwiftUI 带有 CustomView 的 onTapGesture 不刷新子视图 - onTapGesture with CustomView not Refreshing child view 视图 SwiftUI 的 onTapgesture 禁用按钮 - onTapgesture disabling buttons of View SwiftUI 当视图位于 ScrollView 内时,onTapGesture 不起作用? - onTapGesture not working when view is inside ScrollView? iOS:从另一个视图以编程方式调用 onTapGesture - iOS : Programmatically call onTapGesture from another view UI.onTapGesture如何返回原始输入swift - How to return to the original input swift UI .onTapGesture SwiftUI 处理带有释放动作的 onTapGesture 和 onLongPressGesture 的按钮/视图 - SwiftUI handling button/view with onTapGesture and onLongPressGesture with a release action 我无法在 SwiftUI 中使用.onAppear()、.onTapGesture() 函数和 NavigationLink 更改值 - I can not change the value with .onAppear(), .onTapGesture() function and with a NavigationLink in SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM