1

I have a very simple piece of code that shows correctly in the swiftui static preview, and runs correctly in iOS. However when I select the macOS target and the live preview, The entire hierarchy moves repeatedly down from the top corner.

This is the code I'm experimenting with (Using Xcode Version 12.3 (12C33)):

//
//  BrokenAnimation.swift
//

import SwiftUI

struct BrokenAnimation: View {
    @State var phase: CGFloat = 0
    
    var body: some View {
        ZStack(alignment: Alignment.topLeading) {
            Rectangle()
                .fill(Color.blue)
                .frame(width: 1200, height: 1200)
            Rectangle()
                .stroke(style: StrokeStyle(lineWidth: 2, lineCap: .butt, lineJoin: .round, miterLimit: 1, dash: [5], dashPhase: phase))
                .frame(width: 100, height: 100, alignment: Alignment.topLeading)
                .foregroundColor(.white)
                .onAppear {
                    withAnimation(Animation.linear.repeatForever(autoreverses: false)) {
                        self.phase += 10
                    }
                }
        }
    }
}

struct BrokenAnimation_Previews: PreviewProvider {
    static var previews: some View {
        BrokenAnimation()
    }
}

My expectation is that it should draw a solid blue rectangle from the top left corner of the screen (1200x1200). It should then draw a white rectangle on top of it, also at the top left corner, with a size of 100x100. The white rectangle is stroked with a dashed line, which should animate its phase - I'm expecting to see a crawling-ants effect on the dashed rectangle.

However when I select the "Live Preview" for MacOS it makes a window with the expected blue rectangle and white dotted line. But the entire ZStack moves repeatedly down from the top left corner.

Is this a bug in swiftui on macOS? or is there something I'm missing here?

I observe similar behavior if I use a VStack instead of the ZStack.

TirantFox
  • 11
  • 2
  • Some animations may be broken in previews. Try on a real device/simulator instead. I checked that your code works for me on my Mac and in simulator. – pawello2222 Jan 06 '21 at 00:01

0 Answers0