Swift IOS自定义activity indicator(例子)

・2 分钟阅读

默认activity indicator

  • 下面的函数会默认activity indicator添加到视图,

func showActivityIndicatory(uiView: UIView) {
 var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
 actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
 actInd.center = uiView.center
 actInd.hidesWhenStopped = true
 actInd.activityIndicatorViewStyle =
 UIActivityIndicatorViewStyle.WhiteLarge
 uiView.addSubview(actInd)
 activityIndicator.startAnimating()
}
  • 输出将如下所示

Picture

自定义activity indicator

  • 将自定义activity indicator添加到视图中

func showActivityIndicatory(uiView: UIView) {
 var container: UIView = UIView()
 container.frame = uiView.frame
 container.center = uiView.center
 container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.3)

 var loadingView: UIView = UIView()
 loadingView.frame = CGRectMake(0, 0, 80, 80)
 loadingView.center = uiView.center
 loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
 loadingView.clipsToBounds = true
 loadingView.layer.cornerRadius = 10

 var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
 actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
 actInd.activityIndicatorViewStyle =
 UIActivityIndicatorViewStyle.WhiteLarge
 actInd.center = CGPointMake(loadingView.frame.size.width / 2,
 loadingView.frame.size.height / 2);
 loadingView.addSubview(actInd)
 container.addSubview(loadingView)
 uiView.addSubview(container)
 actInd.startAnimating()
}
  • 在这个函数中,我增加了部分透明叠加来查看和显示圆角矩形中的activity indicator,

  • 下面是输出

Picture

  • 完整源代码

https://github.com/erangaeb/dev-notes/blob/master/swift/ViewControllerUtils.swift

讨论
Zxzx8082 profile image