简体   繁体   English

SwiftUI 动画中断屏幕旋转

[英]SwiftUI animations break screen rotation

I've searched high and low, but I can't seem to find any mention of this issue anywhere on the internet.我搜索了高低,但我似乎在互联网上的任何地方都找不到任何提到这个问题的地方

Whenever any SwiftUI animation is playing, and the screen rotates, the orientation change animation is broken.每当播放任何SwiftUI animation 并且屏幕旋转时,方向更改 animation 就会损坏。

Instead of gracefully transitioning to the new orientation, my views snap to their final positions, leaving large amounts of white space outside the rendered area while the rest of the iOS screen rotating animation plays out.我的视图并没有优雅地过渡到新的方向,而是捕捉到它们的最终位置,在渲染区域之外留下大量空白空间,而 iOS 屏幕旋转 animation 的 rest 播放出来。

This is very apparent in my app, because I've got a non-white background color, and an animation that is usually playing.这在我的应用程序中非常明显,因为我有一个非白色的背景颜色,以及一个通常正在播放的 animation。

I've tried this with my regular code, with a simplified test view, and with some sample code downloaded from a few tutorials, and the behavior always seems to be there.我已经用我的常规代码、简化的测试视图以及从一些教程下载的一些示例代码进行了尝试,并且这种行为似乎总是存在的。 If no animations are playing, the screen rotates fine.如果没有播放动画,则屏幕旋转正常。 If there are animations, the rotation is broken and the view elements jerk to their final position.如果有动画,则旋转被破坏,并且视图元素猛拉到最终的 position。

Has anyone else experienced this?有没有其他人经历过这个? I literally can't find a mention of this issue anywhere, and I don't know where to even begin with it.从字面上找不到任何地方提到这个问题,我什至不知道从哪里开始。

Here's an example of the "simplified test view" I made.这是我制作的“简化测试视图”的示例。 Please excuse the annoying VStack/Hstack/Spacer combo, it was just a quick way to fill the screen with an edge-to-edge background color.请原谅烦人的 VStack/Hstack/Spacer 组合,这只是用边到边背景颜色填充屏幕的快速方法。

struct squareView: View {
    @State var rotationDegrees = 0.0
    
    var body: some View {
        VStack {
            Spacer()
            HStack {
                Spacer()
                Button(action: {
                    withAnimation(.linear(duration: 3)){
                        rotationDegrees += 180
                    }
                }){
                    Rectangle()
                        .fill(Color.white)
                        .cornerRadius(30)
                        .rotationEffect(.degrees(rotationDegrees))
                        .frame(width: 250, height: 250)
                }
                Spacer()
            }
            Spacer()
        }
        .background(Color.black).edgesIgnoringSafeArea(.all)
    }
}

I'd really appreciate it if anyone has any information to share on this.如果有人对此有任何信息可以分享,我将不胜感激。 A solution, a workaround, or even just a general direction to look in.一个解决方案,一个变通方法,甚至只是一个大致的方向。

Anything would help.任何事情都会有所帮助。 Thanks.谢谢。


Edit: Since maybe this is a hard one to explain in words, I've recorded a video of the issue.编辑:因为这可能很难用语言来解释,所以我录制了这个问题的视频。

https://youtu.be/UejK_MTY4Mw https://youtu.be/UejK_MTY4Mw

As you can see, when the box is stationary, the screen will rotate just fine, but if it's spinning at all, the animation of iOS changing orientation breaks badly .如您所见,当盒子静止时,屏幕会旋转得很好,但如果它完全旋转,iOS 的 animation 改变方向会严重损坏。

(Also, in hindsight, a rotating box was not the best choice of animation when trying to explain an issue with screen rotation. The animation of the box is inconsequential, and the "rotation" I'm having an issue with is that of the device screen changing orientation.) (另外,事后看来,在试图解释屏幕旋转问题时,旋转盒子并不是 animation 的最佳选择。盒子的 animation 无关紧要,我遇到的“旋转”问题是设备屏幕改变方向。)

While the video shows it best, here's some screenshots that attempt to convey the issue.虽然视频显示得最好,但这里有一些截图试图传达这个问题。

No Animation:没有 Animation:

在此处输入图像描述

With Animation:使用 Animation:

在此处输入图像描述

we have to change this to black color.我们必须将其更改为黑色。

在此处输入图像描述

Just wanted to update this since someone seems to have found it recently and was asking (elsewhere).只是想更新它,因为最近有人似乎发现了它并且正在询问(在其他地方)。

This issue was fixed by Apple in iOS 15 . Apple 在 iOS 15 中修复了此问题

I reported the issue to them and they acknowledged it as a bug in SwiftUI itself.我向他们报告了这个问题,他们承认这是 SwiftUI 本身的一个错误。

I didn't hear anything after that until just before iOS 15 was released.直到 iOS 15 发布之前,我才听到任何消息。

iOS 15 fixes this bug. iOS 15 修复了这个错误。 I didn't even have to rebuild my test app.我什至不必重建我的测试应用程序。 It just worked as of the update.它只是更新时起作用。

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

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