SwiftUI动画影响其他视图

3

这是我的视图:

struct HomeView: View {
    @ObservedObject var instance = Instance()
    @State var dotColor = Color.gray
    var repeatingAnimation: Animation {
            Animation
                .easeInOut(duration: 1.0)
                .repeatForever()
        }
    var body: some View {
        ZStack {
            TabView(selection: $selectedTab) {
                Settings(auth: auth)
                .tabItem {
                        Image(systemName: "gear")
                    Text("Settings")
    
                }.tag(0)
                ZStack {
                    MapView(locationManager: locationManager)
                    Color.black.opacity(self.instance.status.opacity)
                    VStack {
                        Spacer()
                        Button(action: {
                            print("clicked")
                            withAnimation(self.repeatingAnimation) {
                                                self.dotColor = Color.white
                                            }
                            self.instance.changeStatus()
                        }){
                            HStack {
                                Text(self.instance.status.text)
                                    .font(.system(size: 24))
                                    .fontWeight(.bold)
                                    .foregroundColor(Color.white)
                                Circle().frame(width: 12, height: 12)
                                    .foregroundColor(self.dotColor)
                                    .colorMultiply(self.dotColor)
                                    .overlay(
                                        Circle()
                                            .stroke(Color.black, lineWidth: 1)
                                    )
                                
                            }
                        }.padding()

我的动画每秒钟将Circle()的颜色从gray变为white。然而,它也会每秒钟改变我的ZStack颜色不透明度(Color.black.opacity(self.instance.status.opacity)) - 即使它与我的动画无关。

如果我移除动画,这种奇怪的行为就不会发生在ZStack的不透明度上。

我该如何解决这个问题?

1个回答

1
您可以明确地禁用某些修饰符的动画效果,例如:
Color.black.opacity(self.instance.status.opacity)
  .animation(nil)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接