在许多 VC 中使用活动指示器而不快速复制代码
Posted
技术标签:
【中文标题】在许多 VC 中使用活动指示器而不快速复制代码【英文标题】:use activity indicator in many VC without duplicating code swift 【发布时间】:2016-07-19 11:50:44 【问题描述】:我在 Swift ios 中有两个 ViewControllers
(A 和 B)。 A 和 B 都从 Internet 加载数据(分别)。我想在加载时显示activityIndicator
。我知道我可以通过在每个 VC 中按如下方式声明一次来做坏事
ViewController A
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
func activityIndicatorBegin()
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
disableUserInteraction()
greyView = UIView()
greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
greyView.backgroundColor = UIColor.blackColor()
greyView.alpha = 0.5
self.view.addSubview(greyView)
func activityIndicatorEnd()
self.activityIndicator.stopAnimating()
enableUserInteraction()
self.greyView.removeFromSuperview()
对 ViewController B 执行完全相同的操作,并调用在 B 视图控制器中声明的 activityIndicatorBegin 和 activityIndicatorEnd 函数。但是,我想让代码更干净。怎么可能做到?这些天我正在努力使代码更干净。
谢谢,
--更新--
我想像下面的代码这样的东西会起作用。但这不是因为我不能在扩展中声明变量
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
extension UIViewController
func activityIndicatorBegin()
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
disableUserInteraction()
greyView = UIView()
greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
greyView.backgroundColor = UIColor.blackColor()
greyView.alpha = 0.5
self.view.addSubview(greyView)
func activityIndicatorEnd()
self.activityIndicator.stopAnimating()
enableUserInteraction()
self.greyView.removeFromSuperview()
【问题讨论】:
【参考方案1】:SWIFT 4
extension UIView
func activityStartAnimating(activityColor: UIColor, backgroundColor: UIColor)
let backgroundView = UIView()
backgroundView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
backgroundView.backgroundColor = backgroundColor
backgroundView.tag = 475647
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
activityIndicator = UIActivityIndicatorView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
activityIndicator.center = self.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.color = activityColor
activityIndicator.startAnimating()
self.isUserInteractionEnabled = false
backgroundView.addSubview(activityIndicator)
self.addSubview(backgroundView)
func activityStopAnimating()
if let background = viewWithTag(475647)
background.removeFromSuperview()
self.isUserInteractionEnabled = true
用法:
self.view.activityStartAnimating(activityColor: UIColor.white, backgroundColor: UIColor.black.withAlphaComponent(0.5))
self.view.activityStopAnimating()
【讨论】:
【参考方案2】:创建一个BaseViewController
并添加这两个方法并在BaseViewController
中声明activityIndicator
对象。现在将BaseViewController
设为所有ViewController
的父类。之后,如果您想添加指标,只需像这样调用
1) 创建 BaseViewController
class BaseViewController: UIViewController
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
override func viewDidLoad()
super.viewDidLoad()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func activityIndicatorBegin()
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
disableUserInteraction()
greyView = UIView()
greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
greyView.backgroundColor = UIColor.blackColor()
greyView.alpha = 0.5
self.view.addSubview(greyView)
func activityIndicatorEnd()
self.activityIndicator.stopAnimating()
enableUserInteraction()
self.greyView.removeFromSuperview()
2) 现在将这个BaseViewController
指定为你所有viewController
的父级,就像这样
class ViewController1: BaseViewController
override func viewDidLoad()
super.viewDidLoad()
//Now if you want to add activityIndicator call like this
self.activityIndicatorBegin()
//or if you want remove activityIndicator
self.activityIndicatorEnd()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【讨论】:
这是否意味着我需要在情节提要中物理创建另一个(第三个)VC 并将其分配给 BaseViewController 并且它什么也不做?我对我的想法有一些更新 谢谢,它成功了。所以基本上不是做扩展:UIviewcontroller,我把它改成类并让另一个类继承它。智能 欢迎。但是extension
也很有帮助,如果你想要像UITextField
、UIButton
、UILabel
等带有一些自定义属性的控件,那么extension
是一个不错的选择。希望你能明白。快乐编码。【参考方案3】:
您是否尝试使用可以在 A 类和 B 类中复制的所有代码创建一个基类?如果这些类正在做完全不同的事情,您可以为活动指示器添加扩展
【讨论】:
一个VC是tableView,另一个是collectionView。我该如何做扩展。请参阅上面的更新。谢谢 @EdwardHung 你有大量的全球活动指标示例mpclarkson.github.io/2016/01/06/…以上是关于在许多 VC 中使用活动指示器而不快速复制代码的主要内容,如果未能解决你的问题,请参考以下文章