如何简化这个长的 UIbutton 代码?

How do I simplify this long UIbutton code?

我尝试创建 5 个 UIbutton(实际上是 12 个,但代码太长 post 所以我剪到 5 个。都是一样的)并使它的角变圆并添加阴影

但我认为有更好的方法来简化这段长代码

请帮忙。谢谢

class ViewController: UIViewController {

@IBOutlet weak var button: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var button5: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
       
    button.layer.cornerRadius = 35
    button.layer.shadowRadius = 5
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOpacity = 0.5
    button.layer.shadowOffset = CGSize(width:0, height:1)
    button.layer.masksToBounds = false
    
    button2.layer.cornerRadius = 35
    button2.layer.shadowRadius = 5
    button2.layer.shadowColor = UIColor.black.cgColor
    button2.layer.shadowOpacity = 0.5
    button2.layer.shadowOffset = CGSize(width:0, height:1)
    button2.layer.masksToBounds = false
    
    button3.layer.cornerRadius = 35
    button3.layer.shadowRadius = 5
    button3.layer.shadowColor = UIColor.black.cgColor
    button3.layer.shadowOpacity = 0.5
    button3.layer.shadowOffset = CGSize(width:0, height:1)
    button3.layer.masksToBounds = false
    
    button4.layer.cornerRadius = 35
    button4.layer.shadowRadius = 5
    button4.layer.shadowColor = UIColor.black.cgColor
    button4.layer.shadowOpacity = 0.5
    button4.layer.shadowOffset = CGSize(width:0, height:1)
    button4.layer.masksToBounds = false
    
    button5.layer.cornerRadius = 35
    button5.layer.shadowRadius = 5
    button5.layer.shadowColor = UIColor.black.cgColor
    button5.layer.shadowOpacity = 0.5
    button5.layer.shadowOffset = CGSize(width:0, height:1)
    button5.layer.masksToBounds = false

创建一个方法并调用它很简单

[button,button2].forEach({self.createButton([=10=])})

func createButton(_ button: UIButton, radius: CGFloat = 35, shadowRadius: CGFloat = 5, shadowColor: UIColor = UIColor.black, shadowOpacity: Float = 0.5, shadowOffSet: CGSize = CGSize(width: 0, height: 1)){
    
    button.layer.cornerRadius = radius
    button.layer.shadowRadius = shadowRadius
    button.layer.shadowColor = shadowColor.cgColor
    button.layer.shadowOpacity = shadowOpacity
    button.layer.shadowOffset = shadowOffSet
    button.layer.masksToBounds = false
    
}

你可以简单地使用数组

[button1,button2..].forEach { button in
    button.layer.cornerRadius = 35
    button.layer.shadowRadius = 5
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOpacity = 0.5
    button.layer.shadowOffset = CGSize(width:0, height:1)
    button.layer.masksToBounds = false
}

或者使用方法,如果你需要为不同的按钮传递不同的参数

setupButton(button1)
setupButton(button2)

func setupButton(_ button: UIButton) {
    button.layer.cornerRadius = 35
    button.layer.shadowRadius = 5
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOpacity = 0.5
    button.layer.shadowOffset = CGSize(width:0, height:1)
    button.layer.masksToBounds = false
}

创建一个函数来完成重复性工作。像这样:

@IBOutlet weak var button: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var button5: UIButton!

//...
func configure(button: UIButton) {
    button.layer.cornerRadius = 35
    button.layer.shadowRadius = 5
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOpacity = 0.5
    button.layer.shadowOffset = CGSize(width:0, height:1)
    button.layer.masksToBounds = false
}

[button, button2, button3, button4, button5].forEach { configure(button:[=10=]) }

您始终可以继承 UIButton 并创建您自己的自定义按钮:

class CustomButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    func setup() {
        layer.cornerRadius = 35
        layer.shadowRadius = 5
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.5
        layer.shadowOffset = CGSize(width:0, height:1)
        layer.masksToBounds = false
    }
}

@IBOutlet weak var button: CustomButton!
@IBOutlet weak var button2: CustomButton!
@IBOutlet weak var button3: CustomButton!
@IBOutlet weak var button4: CustomButton!
@IBOutlet weak var button5: CustomButton!

也许你可以在 swift 5.3 中使用可变参数尝试这个

@IBOutlet weak var button: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var button5: UIButton!

//...
func configure(buttons: UIButton...) {
    for button in buttons {
        button.layer.cornerRadius = 35
        button.layer.shadowRadius = 5
        button.layer.shadowColor = UIColor.black.cgColor
        button.layer.shadowOpacity = 0.5
        button.layer.shadowOffset = CGSize(width:0, height:1)
        button.layer.masksToBounds = false
    }
}
configure(button, button1, button2, button3, button4, button5)