【问题标题】:Offset doesn't reset after onTapGesture - SwiftUIonTapGesture 后偏移量不会重置 - SwiftUI
【发布时间】:2020-07-21 10:38:07
【问题描述】:

我正在尝试创建一张卡片,当它被点击时会滑动,然后回到原始位置而不再次点击 点击手势的 .onEnded() 函数不起作用

这是测试代码

struct ContentView: View {

@State var tapped = false
var body: some View {
    VStack {
        ZStack {
            Rectangle()
                .fill(Color.green)
                .frame(height: 300)
            
            Rectangle()
                .fill(Color.red)
                .frame(width: 290, height: 64)
                .offset(y: tapped ? 118 : 0)
                .onTapGesture {
                    self.tapped.toggle()
            }
            .animation(.easeInOut)
        }
        
        Spacer()
    }.edgesIgnoringSafeArea(.all)
}

}

【问题讨论】:

    标签: ios swift animation swiftui


    【解决方案1】:

    应用的属性不会自行神奇地重置 - 您需要根据需要更改它们。

    这是最简单的解决方案。使用 Xcode 11.4 / iOS 13.4 测试

    var body: some View {
        VStack {
            ZStack {
                Rectangle()
                    .fill(Color.green)
                    .frame(height: 300)
    
                Rectangle()
                    .fill(Color.red)
                    .frame(width: 290, height: 64)
                    .offset(y: tapped ? 118 : 0)
                    .onTapGesture {
                        self.tapped.toggle()
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                            self.tapped.toggle()
                        }
                }
                .animation(Animation.easeInOut(duration: 0.5))
            }
    
            Spacer()
        }.edgesIgnoringSafeArea(.all)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2018-10-08
      • 2021-04-03
      • 2021-09-03
      • 2018-01-30
      • 1970-01-01
      相关资源
      最近更新 更多