@FocusState private var focus: Bool
OtherView()
ScrollView {
ViewA()
.focused($focus)
.onChange(of:focus) { ... }
ViewB()
ViewC()
}
以上寫法會引起大面積重繪,只要在 ScrollView 產生焦點,就會引起大面積重新繪製。但是在這個案例下,ViewC 是不會重新繪製的。但是 ViewC 內的焦點變換會導致 A 和 B 、OtherView 重繪。
為 ViewA() 建立一個獨立的 struct …: View { } View。把 .focused + .onChange 轉移到內部即可。
ScrollView {
ViewA()
ViewB()
ViewC()
}
// ------- New View struct
@FocusState private var focus: Bool
struct ViewA: View {
MyView()
.focused($focus)
.onChange(of:focus) { ... }
}
問題解決之前
使用解決辦法之後