【问题标题】:How do you exclude a specific UIButton from an extension in Swift?如何从 Swift 的扩展中排除特定的 UIButton?
【发布时间】:2021-05-03 01:04:44
【问题描述】:

我是初学者,请耐心等待。

我创建了一个基本的计时器应用程序并将两个按钮的角弄圆了。该按钮在圆圈外仍然可以点击,我需要解决这个问题,所以我在网上找到了一个解决方案,将代码插入到 UIButton 的子类或扩展中。我找到了两种方式的解决方案并选择了扩展选项,因为代码更易于阅读并且我可以更好地理解它。

现在的问题是我有第三个 UIButton 受到扩展的影响,我想排除它。我不确定这是否实用(或可能),所以如果有更好的方法来解决这个问题,请纠正我。我需要从扩展中排除的按钮是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)"
    }
    

}

【问题讨论】:

  • “现在的问题是我有第三个 UIButton 受到扩展的影响,我想排除它。”在这种情况下,扩展是一个糟糕的主意。创建您自己的 UIButton 子类,并使用您找到的其他解决方案。

标签: ios swift iphone xcode uikit


【解决方案1】:

对于您的情况,扩展不是正确的选择,因为从扩展调用的方法将应用于类型本身(在这种情况下为所有 UIButton 对象)。

您的一个选择是创建UIButton 的子类而不是扩展。像这样的:

class RoundedButton: UIButton {
  open override func draw(_ rect: CGRect) {
      self.layer.cornerRadius = 50.0
      self.layer.masksToBounds = true
  }
  private var touchPath: UIBezierPath {return UIBezierPath(ovalIn: self.bounds)}
  open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
      return touchPath.contains(point)
  }
}

因此您可以选择应该从上面的类继承自定义布局的按钮:

@IBOutlet weak var startButton: RoundedButton!
@IBOutlet weak var stopButton: RoundedButton!
@IBOutlet weak var resetButton: UIButton! // Will not get the style applied for startButton and stopButton

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多