Home > Back-end >  How to update this animated view for iOS 15
How to update this animated view for iOS 15

Time:01-15

I have a "message bubble" that "springs" in from one side of the screen. I use the following code:

struct MessageView: View, Identifiable {
  let id = UUID()
  var side: MessageBubbleSide = .left
  let innerView: AnyView
  var hasBackground = true

  var body: some View {
    HStack {
      innerView
        .padding(.horizontal, .five)
        .padding(.vertical, .four)
        .messageBubble(
          displayFrom: side,
          hasBackground: hasBackground
        )
      Spacer()
    }
    .transition(.move(edge: side == .left ? .leading : .trailing))
    .animation(.spring()) 
  }
}

This works, but when building for iOS 15, it says 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.. I usually know how to do this in other circumstances, but haven't been able to solve it when wanting a spring animation as the view appears on screen. Anyone know how to do this?

CodePudding user response:

You need to add a value which activates animation (aka, dependency). As you animate transition dependent on side, then it should be

.transition(.move(edge: side == .left ? .leading : .trailing))
.animation(.spring(), value: side)     // << here !!

CodePudding user response:

All I did was use the newer API which wants a value, where I just threw in a UUID:

.animation(.linear, value: UUID())
  •  Tags:  
  • Related