view.actOnSelfWidth()
import SwiftUI
extension View {
/// โญ usage: `view.actOnSelfWidth { width in ... }`
public func actOnSelfWidth(
action: @escaping (CGFloat) -> Void
) -> some View
{
self.background( GeometryReader { geo in
Color.clear
// โญ ไธๅฑคๅๅ ฑ่ชๅทฑ็ width
.preference(key: ViewPreference.Width.self, value: geo.size.width)
})
// โญ ไธๅฑคๅพ็ฅ width๏ผไธฆ็จ `action` ่็ใ
.onPreferenceChange(ViewPreference.Width.self, perform: action)
}
}
uses ViewPreference
Last updated