简体   繁体   English

基于 Apple Watch 的 Always On Scenen 阶段切换 Animation 未按预期工作?

[英]Toggling Animation based on Apple Watch's Always On Scenen Phase not working as expected?

I may be doing something wrong, but following Apple's Docs for how to pause animation to support Always On in WatchOS 8, with the code below my animation will pause correctly when the the scene phase is inactive, but when it becomes active again the animation does not resume?我可能做错了什么,但是按照Apple 的文档了解如何暂停 animation 以支持 WatchOS 8 中的 Always On,我的 animation 下面的代码将在场景阶段处于非活动状态时正确暂停,但当它再次变为活动状态时 Z6F1C25ED1952596不恢复?

struct PulseView: View {
    
    @Environment(\.scenePhase) private var scenePhase
    
    @State var animate = false
    
    var body: some View {
        if scenePhase == .active {
                ZStack {
                    ZStack {
                        Circle().fill(circlesColor().opacity(0.25)).frame(width: outerCircleSize, height: outerCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                        Circle().fill(circlesColor().opacity(0.35)).frame(width: middleCircleSize, height: middleCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                        Circle().fill(circlesColor()).frame(width: innerCircleSize, height: innerCircleSize)
                    }
                    .onAppear { self.animate = true }
                    .animation(animate ? Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true) : .default)
                   
          } else {
                  ZStack {
                      ZStack {
                          Circle().fill(circlesColor().opacity(0.25)).frame(width: outerCircleSize, height: outerCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                          Circle().fill(circlesColor().opacity(0.35)).frame(width: middleCircleSize, height: middleCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                          Circle().fill(circlesColor()).frame(width: innerCircleSize, height: innerCircleSize)
                      }
                      //.onAppear { self.animate = true }
                      //.animation(animate ? Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true) : .default)
          }
     
    }

I should have realized why it wasn't working, but adding a toggle to animate using an OnChangeOf modifier solved this issue.我应该已经意识到它为什么不起作用,但是使用OnChangeOf修饰符添加一个切换animate解决了这个问题。

 struct PulseView: View {
        
        @Environment(\.scenePhase) private var scenePhase
        
        @State var animate = false
        
        var body: some View {
            if scenePhase == .active {
                    ZStack {
                        ZStack {
                            Circle().fill(circlesColor().opacity(0.25)).frame(width: outerCircleSize, height: outerCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                            Circle().fill(circlesColor().opacity(0.35)).frame(width: middleCircleSize, height: middleCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                            Circle().fill(circlesColor()).frame(width: innerCircleSize, height: innerCircleSize)
                        }
                        .onAppear { self.animate = true }
                        //HERE BELOW
                        .onChange(of: scenePhase, perform: { newValue in
                          if newValue == .active {
                             self.animate = true
                          } else {
                             self.animate = false
                          }
                         })
                        .animation(animate ? Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true) : .default)
                       
              } else {
                      ZStack {
                          ZStack {
                              Circle().fill(circlesColor().opacity(0.25)).frame(width: outerCircleSize, height: outerCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                              Circle().fill(circlesColor().opacity(0.35)).frame(width: middleCircleSize, height: middleCircleSize).scaleEffect(self.animate ? 1 : 0.01)
                              Circle().fill(circlesColor()).frame(width: innerCircleSize, height: innerCircleSize)
                          }
                          //.onAppear { self.animate = true }
                          //.animation(animate ? Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true) : .default)
              }
         
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM