【问题标题】:Single Up/Down Animation on SwiftUISwiftUI 上的单上/下动画
【发布时间】:2021-07-07 03:39:04
【问题描述】:

我正在尝试制作一个动画,其中硬币翻转并向上移动,然后返回并在单击按钮后停止。使用此代码,它会上下移动,但在动画结束后会捕捉到顶部位置。我知道这一定是因为“.offset”,但我不知道解决方法。 Youtube 帮助不大。

   @State private var isClicked : Bool = false

            Image(coins[numbers[0]])
                .resizable()
                .scaledToFit()
                .scaleEffect(0.6)
                .rotation3DEffect(isClicked ?
                                    .init(degrees: 1080) : .init(degrees: 0),
                                  axis: (x: 10.0, y: 0.0, z: 0.0))
                .animation(Animation.easeInOut.repeatCount(2, autoreverses: true)
                            .speed(0.5))
                .offset(y: isClicked ? -200 : 0)
            
           Button(action: {self.animation()}, label: {
                Text("FLIP!")
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(Color.black)
            })

    func animation() {
        self.isClicked.toggle()
    }

【问题讨论】:

    标签: iphone xcode swiftui swiftui-animation


    【解决方案1】:

    你应该使用Animation.easeInOut.repeatCount(3, autoreverses: true, .speed(0.5) 其中有repeatCount(3 而不是repeatCount(2。硬币上升和下降,那就是它已经完成了您声明的 2 个重复周期。在那之后,硬币应该在顶部,但它已经完成了 2 次重复计数,因此它无法动画到顶部,它只是在没有任何动画的情况下到达顶部。使用repeatCount(3,您又添加了一个循环,因此硬币有另一个机会回到顶部并在那里结束动画。

    【讨论】:

    • 休息了一段时间,但我回来了。我不希望动画在顶部结束,我需要它在仅 2 个动作开始的地方结束;先上升,再下降并停在原来的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多