I want to automatically Trigger and Reset Custom Color Picker in SwiftUI
I have a custom color picker in a SwiftUI view. When you click on the color picker icon, a draggable circle appears, allowing you to pick the color under it when you release. I have attached a video to demonstrate this behavior.
What I Want:
When the app starts, I want that draggable circle to be already present, as if the user has already tapped the color picker icon.
After the user selects a color and the color is processed, I want the circle to appear again automatically, allowing the user to pick another color.
What I Have Tried:
I thought I could achieve this by accessing a UIButton somewhere in UIColorPickerViewController, but I haven’t been able to locate that button.
func updateUIViewController(_ uiViewController: UIColorPickerViewController, context: Context) {
if pickerIsActive {
triggerColorPicker(uiViewController)
}
}
func triggerColorPicker(_ uiViewController: UIColorPickerViewController) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if let button = findButton(in: uiViewController.view) {
print("Button found: (button)")
button.sendActions(for: .touchUpInside)
} else {
print("Button not found")
}
}
}
func triggerColorPicker(in view: UIView) -> UIView? {
for subview in view.subviews {
if subview is UIButton || subview is UIControl {
return subview // Return any UIControl or UIButton found
} else if let foundButton = findButton(in: subview) {
return foundButton
}
}
return nil
}
func printViewHierarchy(in view: UIView, level: Int = 0) {
let prefix = String(repeating: " ", count: level * 2)
print("(prefix)(view.self)")
for subview in view.subviews {
printViewHierarchy(in: subview, level: level + 1)
}
}
Question:
How can I programmatically simulate the initial tap on the color picker icon when the app starts and make the draggable circle appear again automatically after the user selects a color?
Any help or guidance would be appreciated. Thank you