我已经设法创建了一个 UIActivityIndicatorView
扩展,用于将其设置为动画
以启动和停止。但是,我想通过使用 get
和 set
使用 bool 类型的 computed property
来让它变得更好。我试过但想不出办法。我怎么能重构这个。
extension UIActivityIndicatorView {
func loadingIndicator(_ isLoading: Bool) {
if isLoading {
self.startAnimating()
} else {
self.stopAnimating()
}
}
}
最佳答案
您可以使用 UIActivityIndicatorView
的 isAnimating
属性作为 isLoading
的支持值。您只需要确保您在 setter 中正确控制开始/停止动画,这将设置 isAnimating
,因此 isLoading
也将正确设置。
extension UIActivityIndicatorView {
var isLoading:Bool {
get {
return isAnimating
} set {
if newValue {
self.startAnimating()
} else {
self.stopAnimating()
}
}
}
}
关于ios - 如何从扩展中的函数创建 UIActivityIndicator 计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53266526/