当用户调用 PhotosPicker 时,我需要直接运行一些代码。我尝试过 .onAppear 和 .onTapGesture,但是当点击打开 PhotosPicker 视图的按钮时,这些方法永远不会被触发。
PhotosPicker(selection: $selectedItem, matching: .any(of: [.images, .not(.livePhotos)])) {
Image(systemName: "photo")
.controlSize(.large)
.foregroundColor(.white)
}
.onTapGesture {
print("--- This is never executed ---")
}
.onChange(of: selectedItem) { newItem in
Task {
if let data = try? await newItem?.loadTransferable(type: Data.self) {
selectedPhotoData = data
}
}
}
Run Code Online (Sandbox Code Playgroud)