Код: Выделить всё
struct TextFieldFocusAnimation: View {
@FocusState private var isFocused: Bool
var body: some View {
HStack {
TextField("", text: .constant(""))
.focused($isFocused)
.border(.red)
if !isFocused {
Button("Sort Button") {
print("Sort clicked")
}
.transition(.move(edge: .leading))
} else {
Button("Cancel") {
isFocused = false
}
.transition(.move(edge: .trailing))
}
}
.padding()
.transformEffect(.identity) // This is to isolate the geometry
.animation(.snappy, value: isFocused)
}
}

Обратите внимание, что кнопки появляются, а не скользят.
Я могу сказать, что переходы обычно ведут себя и при таком коде:

Здесь я использовал @State.
Код: Выделить всё
struct NormalPopInAnimation: View {
@State private var isFocused: Bool = false
var body: some View {
VStack {
HStack {
TextField("", text: .constant(""))
.border(.red)
if !isFocused {
Button("Sort Button") {
print("Sort clicked")
}
.transition(.move(edge: .leading))
} else {
Button("Cancel") {
isFocused = false
}
.transition(.move(edge: .trailing))
}
}
.padding()
.transformEffect(.identity)
Button("Toggle") {
isFocused.toggle()
}
}
.animation(.snappy, value: isFocused)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... -textfield
Мобильная версия