0

I'm using UIViewControllerRepresentable to adjust brightness in SwiftUI. I have 3 buttons for 100%, 50%, 0% brightness. To enable the buttons I've created 3 UIViewControllerRepresentable, 3 UIViewControllers, and 3 struct. How can I combine them, or at least to move button presets to ParentViewController?

`class ParentViewController: UIViewController { var brightness: CGFloat = 1.0

fileprivate func animateView(_ viewToAnimate: UIView) {UIView.animate(withDuration: 0.15, delay: 0, usingSpringWithDamping: 0.5,
       initialSpringVelocity: 0.5, options: .curveEaseIn, animations: {viewToAnimate.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
    }) { (_) in UIView.animate(withDuration: 0.15, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 2, options: .curveEaseIn, animations: {
        viewToAnimate.transform = CGAffineTransform(scaleX: 1, y: 1)}, completion: nil)}}
    
override func viewWillAppear(_ animated: Bool) {super.viewWillAppear(animated)}
override func viewDidAppear(_ animated: Bool) {super.viewDidAppear(animated)}

}

class Bright00Controller: ParentViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 50))
    button.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
    button.setTitle("0%", for: .normal)
    button.setTitleColor(.black, for: .normal)
    button.contentVerticalAlignment = .bottom
    button.layer.cornerRadius = 7
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOffset = CGSize(width: 1.0, height: 3.0)
    button.layer.shadowRadius = 3
    button.layer.shadowOpacity = 1
    button.layer.masksToBounds = false
    
    let constraints = [button.widthAnchor.constraint(equalToConstant: 60), button.heightAnchor.constraint(equalToConstant: 50)]
    
    NSLayoutConstraint.activate(constraints)
    button.addTarget(self, action: #selector(self.brightS0(sender: )), for: .touchUpInside)                         // duplicate

    view.addSubview(button)
    //button.addTarget(self, action: #selector(toggle), for: .touchUpInside)
}
@objc fileprivate func brightS0(sender:UIButton) {print("Brightness 0"); self.animateView(sender)}
@objc func toggle() {UIScreen.main.brightness = CGFloat(0.0); UIScreen.main.brightness = CGFloat(0.01)}

}

struct Bright00: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> Bright00Controller {let vc = Bright00Controller(); return vc} func updateUIViewController(_ uiViewController: Bright00Controller, context: Context) {}} // Brightness 0.0 `

Protocols didn't work.

CodingTh
  • 1
  • 1

0 Answers0