1

My app has a SwiftUI View that contains both a TextEditor to collect a log entry and a List containing past entries.

When the text editor has a non-empty string, a button is presented to process the entry, add it to the data populating the list, and reset the TextEditor. Presently the button appears suddenly whenever text is typed, and I would love to animate that more smoothly, perhaps by fading in the opacity or scaling up the button. However, I can't figure out where to enter that animation.

 @State var newEntryString = "" var body: some View { NavigationView{ VStack{ Text("Create a new log entry.") .padding() TextEditor(text: $newEntryString) .cornerRadius(3.0) .padding() .border(Color.gray, width: 1) .frame(minWidth: 0, idealWidth: 100, maxWidth: .infinity, minHeight: 0, idealHeight: 100, maxHeight: 150, alignment: .center) HStack { Spacer() if !newEntryString.isEmpty{ Button(action: { addEntry() }) { Text("Add Entry") }.buttonStyle(JournalButtonStyle()) .animation(Animation.default.speed(1)) } } Divider() List { ForEach(entrys) { item in Text(item.timestamp!, formatter: entryFormatter).font(.footnote) .foregroundColor(.gray) .padding(.bottom, -10) Text((item.entryString!)) } .onDelete(perform: deleteEntrys) }.onTapGesture { UIApplication.shared.endEditing() } 

1 Answer 1

2

Use animation on container, like

HStack { Spacer() if !newEntryString.isEmpty{ Button(action: { addEntry() }) { Text("Add Entry") }.buttonStyle(JournalButtonStyle()) } }.animation(Animation.default.speed(1)) // << here !! 
Sign up to request clarification or add additional context in comments.

1 Comment

T!hanks for putting me on the right path. It also helped me understand that I needed to place it on the other view affected by the animation—the List further down. Placing the .animation modifier on both those views worked perfectly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.