【问题标题】:SwiftUI animation (animation(_:value:): Newbie questionSwiftUI动画(animation(_:value :):新手问题
【发布时间】:2022-12-09 04:21:36
【问题描述】:

我正在学习 SwiftUI 动画,我有一个新手问题。 .animation() 修饰符已在 iOS 15 中弃用。要使动画正常工作,必须更改“值”。通过我对示例的更改,如果没有“值”,动作会变得不稳定并且不那么流畅。 .animation() 已弃用,但仍会出现警告。

我做对了吗?

示例来自:https://medium.com/apple-developer-academy-federico-ii/drawings-and-animations-in-swiftui-3a2da460e492

struct Example4: View {
    @State private var bounceBall: Bool = false
    @State private var hiddenText: String = "Kick the ball!"
    var body: some View {
        VStack {
            Text(hiddenText)
            Image("ball")
                .resizable()
                .frame(width: 150, height: 150)
                .clipShape(Circle())
                // *Original* 
                .animation(Animation.interpolatingSpring(stiffness: 90, damping: 1.5).repeatForever(autoreverses: false))
                // *Modified*
                .animation(Animation.interpolatingSpring(stiffness: 90, damping: 1.5).repeatForever(autoreverses: false), value: bounceBall)
                .offset(y: bounceBall ? -200 : 200)
                .onTapGesture {
                    self.bounceBall.toggle()
                    self.hiddenText = ""
            }
        }
        .navigationBarTitle("Example 4")
    }
}

【问题讨论】:

  • .animation() 已弃用,您很快就不能使用它,可能在 iOS 17 或 18 中
  • @NhatNguyenDuc 谢谢。是的我明白。这就是为什么我想知道修复方法。当我添加“value: bounceBall”时,动作是不稳定的,从我搜索的所有讨论来看,我的更改似乎应该有效。我的机器有点旧,所以可能是硬件问题?

标签: animation swiftui


【解决方案1】:

在我的 M1 MacBook Air 上,无论是使用已弃用的 .animation() 还是新的 .animation(value:),我都有两种方式的抖动,是的,您正确使用了 value: bounceBall

我对开发也比较陌生,不过我想抛出一个替代 .animation() 的方法来消除警告:

Image("ball")
    .resizable()
    .frame(width: 150, height: 150)
    .clipShape(Circle())
    .offset(y: bounceBall ? -200 : 200)
    .onTapGesture {
        withAnimation(
            Animation.interpolatingSpring(stiffness: 90, damping: 1.5).repeatForever(autoreverses: false)
        ) {
            bounceBall.toggle()
            hiddenText = ""
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2021-03-16
    相关资源
    最近更新 更多