【问题标题】:SwiftUI animation affecting other views影响其他视图的 SwiftUI 动画
【发布时间】:2020-07-04 13:35:44
【问题描述】:

这是我的观点:

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 不透明度不会出现这种奇怪的行为。

我该如何解决这个问题?

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您可以显式禁用某些修饰符的动画,例如

    Color.black.opacity(self.instance.status.opacity)
      .animation(nil)
    

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多