How does scrollIndicatorsFlash(trigger value: some Equatable) not require a binding to observe changes?

I’m learning about Lists and ScrollViews in SwiftUI and one of the modifiers that is available is:

public func scrollIndicatorsFlash(trigger value: some Equatable) -> some View

Documentation

My question is, when I use this, I pass a Bool value, not a Binding<Bool>. How do the scroll views and lists observe that this value has changed if I’m only passing the raw value?

The Bool that I’m passing is a @State variable defined as such: @State var flashIndicators = false. This is very similar to the example that is in the documentation for that method.

How do the scroll views and lists observe that this value has changed if I’m only passing the raw value?

It all starts by SwiftUI calling your view’s body. body returns a view model representing the view you want to create. At the same time, SwiftUI constructs a dependency graph, recording what @States and @Bindings (and other such proeprty wrappers) your view depends on.

When you say .scrollIndicatorsFlash(trigger: flashIndicators), the getter of flashIndicators gets called, and in turn the getter of State.wrappedValue gets called. This is how SwiftUI know that your view depends on the @State flashIndicators.

Therefore, when those dependencies is set, SwiftUI can call your view’s body again, and compare the result with the old view model, and determine what changed. Suppose you set flashIndicators from false to true. SwiftUI knows that body returned a view model with .scrollIndicatorsFlash(trigger: false) last time, and when it called body for the second time (because a dependency has changed), body now returns a view model with .scrollIndicatorsFlash(trigger: true).

SwiftUI does this comparison not only for scrollIndicatorsFlash, but also for many other modifiers. This is how onChange detects a change. This is how animation(_:value:) knows when to animate. This is how things like opacity(_:) knows when to change the opacity, and so on.


scrollIndicatorsFlash doesn’t need to take a Binding, because SwiftUI doesn’t need to change the value you passed in. SwiftUI doesn’t care one bit about the value of the trigger: parameter. All it cares is when the value changes.

Other modifiers like sheet(isPresented:) takes a Binding<Bool> because SwiftUI is designed to set isPresented back to false when the user dismisses the sheet.

2

It didn’t need a Binding<Bool> because it’s just a trigger. Therefore, whenever the input value, which might be any type that conforms Equatable, changes. The scroll indicator will flash. Not the other way around, when the scroll indicator flashes, it will change the input value.

By declaring @State var flashIndicators, you’re telling SwiftUI that every time this variable changes, it will notify to re-render the relevant View, which in this case, is a ScrollView. You can try to toggle this Bool property and then put a debug point on the ScrollView itself to see what occurs.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật