简体   繁体   中英

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.

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.

This is very apparent in my app, because I've got a non-white background color, and an animation that is usually playing.

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.

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.

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

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 .

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

While the video shows it best, here's some screenshots that attempt to convey the issue.

No Animation:

在此处输入图像描述

With 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 .

I reported the issue to them and they acknowledged it as a bug in SwiftUI itself.

I didn't hear anything after that until just before iOS 15 was released.

iOS 15 fixes this bug. I didn't even have to rebuild my test app. It just worked as of the update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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