简体   繁体   English

SwiftUI animation 影响其他视图

[英]SwiftUI animation affecting other views

Here is my View:这是我的观点:

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()

My animation, changes the color of the Circle() from gray to white every second.我的 animation 每秒将Circle()的颜色从gray变为white However, it also changes my ZStack color opacity ( Color.black.opacity(self.instance.status.opacity) ) every second - even though it's not related to my animation.但是,它也会每秒更改我的ZStack颜色不透明度( Color.black.opacity(self.instance.status.opacity) ) - 即使它与我的 animation 无关。

If I remove the animation this weird behaviour does not occur to the ZStack opacity.如果我删除 animation 这种奇怪的行为不会发生在 ZStack 不透明度上。

How can I fix this?我怎样才能解决这个问题?

You can disable animation of some modifier explicitly, like您可以明确禁用某些修饰符的 animation ,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI:输入字段键盘 animation 导致选项卡视图中的其他视图动画 - SwiftUI : Input Field Keyboard animation causing other views in tab view to animate SwiftUI - 带有指向其他视图的链接作为参数的可重用组件 - SwiftUI - reusable components with links to other views as parameters SwiftUI - 水平扩展主网格将我的其他视图推到屏幕外 - SwiftUI - Expanding the main grid horizontally pushes my other views offscreen 以编程方式一个接一个地关闭 SwiftUI 视图不起作用 - Dismiss of SwiftUI Views one after the other programmatically does not work 从 SwiftUI 中的其他/根视图中关闭呈现的工作表 - Dismiss presented sheet from other/root views in SwiftUI 将 SwiftUI 视图覆盖在所有其他视图上,包括工作表 - Overlay SwiftUI view over all other views including sheets 使用 SwiftUI 的 TabbedView 查看切换到其他选项卡时不显示的内容 - Views content not showing on switching to other tabs using TabbedView of SwiftUI SwiftUI 计时器视图暂停屏幕上的所有其他视图 - SwiftUI timer view pausing all other views on screen 在 SwiftUI 中创建自定义导航栏,因此其他 SwiftUI 视图应重用相同的导航栏 - Create a custom Navigation Bar in SwiftUI So other SwiftUI views should reuse same Nav Bar 动画重新放置其他视图后,从其超级视图中删除视图 - Removing a view from it's superview after animation repositions other views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM