为什么SwiftUI动画在iOS和macOS上的行为和对齐方式不同?

为什么SwiftUI动画在iOS和macOS上的行为和对齐方式不同?,swiftui,Swiftui,我启动了一个新的多平台项目(Xcode 12.4)。我写了一个简短的动画贴在下面 鉴于这一简单的swift代码: import SwiftUI @main struct MuiltiPlatApp: App { var body: some Scene { WindowGroup { ContentView() .frame(width: 250, height: 250, alignment: .center)

我启动了一个新的多平台项目(Xcode 12.4)。我写了一个简短的动画贴在下面

鉴于这一简单的swift代码:

import SwiftUI

@main
struct MuiltiPlatApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .frame(width: 250, height: 250, alignment: .center)
                .background(Color.green)
        }
    }
}

struct ContentView: View {
    @State private var animationAmount: CGFloat = 1.0
    var body: some View {
        Color.red
            .clipShape(Circle())
            .overlay(
                Circle()
                    .stroke(Color.blue)
                    .scaleEffect(animationAmount)
                    .opacity(Double(2.0 - animationAmount))
                    .animation(
                        Animation.easeInOut(duration: 2.0)
                            .repeatForever(autoreverses: false)
                    )
            )
            .onAppear{
                self.animationAmount += 1
            }
            .frame(width: 100, height: 100, alignment: .center)
            .background(Color.gray)
    }
}

虽然它在iOS上运行良好,但不幸的是,在macOS上运行相同的代码看起来有所不同

结果是:

我试过几次排列,但都没用。你能告诉我为什么这两种外观不同吗?我怎样才能解决这个问题


提前感谢您的帮助

您还需要指定动画

.animation(
    Animation.easeInOut(duration: 2.0)
        .repeatForever(autoreverses: false),
    value: animationAmount
)

从文件中:

/// Applies the given animation to all animatable values within this view.
///
/// Use this modifier on leaf views rather than container views. The
/// animation applies to all child views within this view; calling
/// `animation(_:)` on a container view can lead to unbounded scope.
///
/// - Parameter animation: The animation to apply to animatable values
///   within this view.
///
/// - Returns: A view that wraps this view and applies `animation` to all
///   animatable values used within the view.
@inlinable public func animation(_ animation: Animation?) -> some View
//-参数:
///-动画:要应用的动画。如果'animation'为'nil',则视图
///没有动画。
///-值:用于监视更改的值。
///
///-返回:每当`值时,将`动画`应用于此视图的视图`
///变化。
@可插入的公共func动画(animation:animation?,值:V)->某些视图,其中V:equalable
如您所见,默认情况下,SwiftUI将给定动画应用于此视图中的所有可设置动画的值


但是,当您传递
值时,只要
发生更改,SwiftUI就会将
动画
应用于此视图。

。。非常感谢你!这是工作。。没关系。。好。。但为什么它在iOS上的效果比在iOS上好呢?你救了我一个晚上@los.adrian它只在这种特殊情况下才起作用——比如试着旋转设备……我发誓我已经试过了!我甚至把它放在iPad上运行,也在上面旋转,那里的行为也很好。。只是不在macOS上。我从未想过这个解决方案。再次感谢!再来一张!如果我将.animation代码片段保留在Circle()处,并使用withAnimation()方法增加self.animationAmount,它仍然可以正常工作。@los.adrian很难说给定视图的所有可设置动画的值到底是什么。如果使用
动画(uquo:value:)
,则可以确保仅当
值更改时,它才会进行动画。否则你可能会遇到意想不到的问题,比如你问题中的问题。谢谢你,但pawello2222发布的内容是有效的。我还没有康复^^"
/// - Parameters:
///   - animation: The animation to apply. If `animation` is `nil`, the view
///     doesn't animate.
///   - value: A value to monitor for changes.
///
/// - Returns: A view that applies `animation` to this view whenever `value`
///   changes.
@inlinable public func animation<V>(_ animation: Animation?, value: V) -> some View where V : Equatable