4

Just can't get my head around this. I have a simple animation which works perfectly. But when I wrap the view into a ScrollView (by uncommenting the 2 lines) it does not animate anymore? Anybody a clue?

import SwiftUI

struct ContentView: View {
    @State var offset = CGSize(width: 0, height: 0)

    var body: some View {
//      ScrollView {
            VStack(spacing: 50) {
                Rectangle()
                    .frame(width: 100, height: 100)
                    .offset(self.offset)
                Button(action: {
                    withAnimation {
                        self.offset.width += 66
                    }
                })
                {
                    Text("Animate me")
                }
            }.frame(width: 300)
//      }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
5
  • I tried your code and it works fine in the simulator. The rectangle animates when it is contained in the ScrollView. The only difference that I noticed was that the button and rectangle were at the top of the screen rather than in the centre. Commented Oct 26, 2019 at 14:04
  • @Andrew copy pasting the code in the question I have the same problem of iTukker: animation won't work if it's inside the ScrollView. Which xCode version are you using? Commented Oct 26, 2019 at 14:06
  • @superpuccio Xcode 11.1, tried it both in the simulator and the preview and it works. Commented Oct 26, 2019 at 14:07
  • @Andrew I'm using that version too. Very strange. I have the same problem of iTukker. Commented Oct 26, 2019 at 14:08
  • I am on Xcode 11.1 as well, and no success. @superpuccio's answer did the trick, I'll accept that one. Still learning SwiftUI and already stumbled on my first animation. Commented Oct 26, 2019 at 14:11

1 Answer 1

1

I've already noticed this behaviour. The issue seems to be the explicit animation. If you go for an implicit animation it works:

struct ContentView: View {
    @State var offset = CGSize(width: 0, height: 0)

    var body: some View {
      ScrollView {
            VStack(spacing: 50) {
                Rectangle()
                    .frame(width: 100, height: 100)
                    .offset(self.offset)
                    .animation(.linear)
                Button(action: {
                    self.offset.width += 66
                })
                {
                    Text("Animate me")
                }
            }.frame(width: 300)
      }
    }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

I haven't managed to get the reason yet, so consider this as a workaround. It may be a SwiftUI bug or something I still can't understand.

Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick, thanx. Still agree that it seems a bug though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.