I am a beginner so bear with me.
I created a basic timer app and rounded the corners of two buttons. The button was still clickable outside of the circle, I needed to fix that so I found a solution online that said to insert the code into the UIButton's subclass or extension. I found solutions for both ways and chose extension option because the code was easier to read and I could understand it better.
The issue now is that I have a third UIButton that is being affected by the extension and I would like to exclude it. I'm not sure if this is even practical (or possible) so please correct me if there is a better way to approach this. The button I need to exclude from the extension is resetButton
.
import UIKit extension UIButton { open override func draw(_ rect: CGRect) { self.layer.cornerRadius = 50.0 self.layer.masksToBounds = true //exclude resetButton??? } private var touchPath: UIBezierPath {return UIBezierPath(ovalIn: self.bounds)} open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { return touchPath.contains(point) } } class ViewController: UIViewController { @IBOutlet weak var label: UILabel! @IBOutlet weak var startButton: UIButton! @IBOutlet weak var stopButton: UIButton! @IBOutlet weak var resetButton: UIButton! var timeRemaining: Int = 10 var timer: Timer? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. //// Moved to an extension in order to //// remove the clickable areas outside //// of the circle //startButton.layer.cornerRadius = 50.0 //stopButton.layer.cornerRadius = 50.0 } @IBAction func start(_ sender: Any) { timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(step), userInfo: nil, repeats: true) } @IBAction func stop(_ sender: Any) { timer?.invalidate() } @IBAction func reset(_ sender: Any) { timer?.invalidate() timeRemaining = 10 label.text = "\(timeRemaining)" } @objc func step() { if timeRemaining > 0 { timeRemaining -= 1 } else { timer?.invalidate() } label.text = "\(timeRemaining)" } }
https://stackoverflow.com/questions/67362432/how-do-you-exclude-a-specific-uibutton-from-an-extension-in-swift May 03, 2021 at 09:04AM
没有评论:
发表评论