ios - 偏移不会在onTapGesture之后重置-SwiftUI

标签 ios swift animation swiftui

我试图创建一张在轻按时会滑动的卡,然后返回到原始位置而无需再次轻按
点按手势的.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)
}
}

最佳答案

应用的属性不会自行神奇地重置-您需要在需要时更改它们。
这是最简单的解决方案。经过Xcode 11.4 / iOS 13.4测试
demo

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)
}

关于ios - 偏移不会在onTapGesture之后重置-SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63012679/

相关文章:

ios - 找不到 Crashlytics.h Xcode 8.0

iphone - iPhone 上的 Chrome 插件

ios - 如何从 ActionSheet 更改 UIButton 的文本

python - Matplotlib 中的像素化动画

ios - 我怎样才能正确地进行停用/事件约束?

ios - 使用枚举和泛型快速扩展类的 Where 子句

python - 将图表重新绘制为视频

c# - 为什么动画高度设置后这个属性直接停止工作

swift - 在非类绑定(bind)协议(protocol)的扩展中,实例必须被视为值类型

ios - 如何将数据转换为URL()?