gpt4 book ai didi

ios - 在 UIButton 上组合扩展和自定义类会产生问题

转载 作者:行者123 更新时间:2023-11-28 13:37:58 25 4
gpt4 key购买 nike

我有一个类和一个 UIButton 的扩展。自定义类删除标题并在单击时显示加载指示器 (LoadingButton)。 - 声明自定义函数以将渐变应用于按钮等的扩展。

两者都在工作,但是在 LoadingButton 类的按钮上调用函数 typeMain() 涉及一个问题 - 按钮的标题没有被删除,因此加载按钮显示在标题的顶部。

如何将两者结合起来解决这个问题?

顺便说一句:button.typeMain()viewDidLoad() 中被调用。

加载按钮:

class LoadingButton: UIButton {
var originalButtonText: String?
var activityIndicator: UIActivityIndicatorView!

func showLoading() {
originalButtonText = self.titleLabel?.text
self.setTitle("", for: .normal)

if (activityIndicator == nil) {
activityIndicator = createActivityIndicator()
}

showSpinning()
}

func hideLoading() {
self.setTitle(originalButtonText, for: .normal)
activityIndicator.stopAnimating()
}

private func createActivityIndicator() -> UIActivityIndicatorView {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.hidesWhenStopped = true
activityIndicator.color = .white
return activityIndicator
}

private func showSpinning() {
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(activityIndicator)
centerActivityIndicatorInButton()
activityIndicator.startAnimating()
}

private func centerActivityIndicatorInButton() {
let xCenterConstraint = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: activityIndicator, attribute: .centerX, multiplier: 1, constant: 0)
self.addConstraint(xCenterConstraint)

let yCenterConstraint = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: activityIndicator, attribute: .centerY, multiplier: 1, constant: 0)
self.addConstraint(yCenterConstraint)
}

自定义每个按钮的扩展:

extension UIButton {

func typeMain() {
self.translatesAutoresizingMaskIntoConstraints = false
let height = UIScreen.main.bounds.height * 0.07
let width = UIScreen.main.bounds.width * 0.9
self.heightAnchor.constraint(equalToConstant: height).isActive = true
self.widthAnchor.constraint(equalToConstant: width).isActive = true

layoutIfNeeded()

self.addCharacterSpacing()
self.tintColor = UIColor.white
let color = UIColor(red: 11/255, green: 95/255, blue: 244/255, alpha: 1)
let sndColor = UIColor(red: 106/255, green: 178/255, blue: 255/255, alpha: 1)

self.layer.cornerRadius = self.frame.size.height / 5.0

self.applyGradient(colours: [color, sndColor], locations: [0.0, 1.0])

let shadowSize : CGFloat = 2.0
self.layer.shadowColor = UIColor(red: 106/255, green: 178/255, blue: 255/255, alpha: 1).cgColor
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.shadowOpacity = 0.4
let shadowPath = UIBezierPath(rect: CGRect(x: -shadowSize / 2,
y: shadowSize,
width: self.frame.size.width + shadowSize,
height: self.frame.size.height + shadowSize))
self.layer.shadowPath = shadowPath.cgPath
self.layer.shadowRadius = 5
self.layer.masksToBounds = false
}

字符间距

extension UILabel {
func addCharacterSpacing(kernValue: Double = 0.5) {
if let labelText = text, labelText.count > 0 {
let attributedString = NSMutableAttributedString(string: labelText)
attributedString.addAttribute(NSAttributedString.Key.kern, value: kernValue, range: NSRange(location: 0, length: attributedString.length - 1))
attributedText = attributedString
}
}

最佳答案

您的自定义类使用按钮标题,扩展方法使用按钮属性标题。更新您的自定义类以保留对 attributedTitle 的引用

class LoadingButton: UIButton {
var originalButtonText: String?
var attributedButtonText: NSAttributedString?
var activityIndicator: UIActivityIndicatorView!

func showLoading() {
originalButtonText = self.titleLabel?.text
attributedButtonText = self.attributedTitle(for: .normal)
self.setTitle("", for: .normal)
self.setAttributedTitle(NSAttributedString(string: ""), for: .normal)

if (activityIndicator == nil) {
activityIndicator = createActivityIndicator()
}
showSpinning()
}
func hideLoading() {
self.setAttributedTitle(attributedButtonText, for: .normal)
self.setTitle(originalButtonText, for: .normal)
activityIndicator.stopAnimating()
}
}

关于ios - 在 UIButton 上组合扩展和自定义类会产生问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56381245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com