Swift 按钮阴影
Swift button shadows
我有两个简单的按钮,都有阴影,并且都对这些阴影使用相同的参数。是否可以将这些参数合并在一起?感觉这样控制起来会简单很多...
这些是按钮:
@IBOutlet weak var locationButton: UIButton!
@IBOutlet weak var infoButton: UIButton!
这是影子的代码:
locationButton.layer.shadowColor = UIColor.black.cgColor
locationButton.layer.shadowOffset = CGSize(width: -1, height: 2)
locationButton.layer.shadowRadius = 1.8
locationButton.layer.shadowOpacity = 0.3
infoButton.layer.shadowColor = UIColor.black.cgColor
infoButton.layer.shadowOffset = CGSize(width: -1, height: 2)
infoButton.layer.shadowRadius = 1.8
infoButton.layer.shadowOpacity = 0.3
再一次,代码运行没有任何问题,我只是想减少同时控制两层的行数。
谢谢!
添加扩展
extension UIView {
func addShadow() {
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: -1, height: 2)
self.layer.shadowRadius = 1.8
self.layer.shadowOpacity = 0.3
}
}
然后调用它
locationButton.addShadow()
您还可以创建一个 UIButton
子类
我有两个简单的按钮,都有阴影,并且都对这些阴影使用相同的参数。是否可以将这些参数合并在一起?感觉这样控制起来会简单很多...
这些是按钮:
@IBOutlet weak var locationButton: UIButton!
@IBOutlet weak var infoButton: UIButton!
这是影子的代码:
locationButton.layer.shadowColor = UIColor.black.cgColor
locationButton.layer.shadowOffset = CGSize(width: -1, height: 2)
locationButton.layer.shadowRadius = 1.8
locationButton.layer.shadowOpacity = 0.3
infoButton.layer.shadowColor = UIColor.black.cgColor
infoButton.layer.shadowOffset = CGSize(width: -1, height: 2)
infoButton.layer.shadowRadius = 1.8
infoButton.layer.shadowOpacity = 0.3
再一次,代码运行没有任何问题,我只是想减少同时控制两层的行数。 谢谢!
添加扩展
extension UIView {
func addShadow() {
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: -1, height: 2)
self.layer.shadowRadius = 1.8
self.layer.shadowOpacity = 0.3
}
}
然后调用它
locationButton.addShadow()
您还可以创建一个 UIButton
子类