设置UIActivityIndi​​catorView后台

时间:2015-09-04 01:28:42

标签: ios swift uiactivityindicatorview

我无法为活动指标背景找到合适的解决方案。我想在视图控制器加载时为活动指示器设置动画,并且为指针后面的整个视图控制器设置灰色透明背景,同时设置动画。一旦指示器停止动画,应删除灰色透明背景,用户应该能够在屏幕上恢复交互。

我认为这是应用程序的标准,但我无法在Swift中找到一个好的解决方案。以下是我到目前为止设置活动指标的代码:

let indicator: UIActivityIndicatorView = UIActivityIndicatorView()
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.White
indicator.hidesWhenStopped = true
indicator.center = self.view.center
self.view.addSubview(indicator)
indicator.startAnimating()

3 个答案:

答案 0 :(得分:1)

您可以为指标背景创建UIView:

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()
}

参考:https://coderwall.com/p/su1t1a/ios-customized-activity-indicator-with-swift

答案 1 :(得分:0)

这就是我所做的,希望它能对其他用户有所帮助。 我正在使用Snapkit进行界面构建

public class GenericView: UIView {

    private var spinerFlag: Bool = false

    internal lazy var spinner: UIActivityIndicatorView = {
        let activity = UIActivityIndicatorView(style: .whiteLarge)
        activity.color = ThemeColor.defaultGreen
        return activity
    }()

    private lazy var spinnerBackground: UIView = {
        let view = UIView()
        view.backgroundColor = ThemeColor.black.withAlphaComponent(0.7)
        view.clipsToBounds = true
        view.layer.cornerRadius = 10
        view.addSubview(self.spinner)
        return view
    }()


    ...


    // MARK: Spinner

    private func setupSpinner(){

        spinnerBackground.isHidden = true
        self.addSubview(spinnerBackground)
        spinnerBackground.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.height.equalTo(spinner.frame.width*2)
        }
        spinner.snp.makeConstraints { make in
            make.centerY.equalTo(spinnerBackground.snp.centerY)
            make.centerX.equalTo(spinnerBackground.snp.centerX)
        }
        self.spinerFlag = true
    }


    internal func spinnerIsHidden(state: Bool){

        if (!self.spinerFlag) {
            setupSpinner()
        }
        spinnerBackground.isHidden = state
    }

}

要运行,您只需实现此UIViewController

public class GenericViewController: UIViewController {

   internal var contentView: GenericView

   public override func loadView() {

      let viewToShow = GenericView()
      self.view = self.contentView = viewToShow
   }

   internal func startSpinner() {
     contentView.spinnerIsHidden(state: false)
     contentView.spinner.startAnimating()
   }

   internal func stopSpinner() {
     contentView.spinnerIsHidden(state: true)
     contentView.spinner.stopAnimating()
   }
}

答案 2 :(得分:0)

我推荐的是

使用 UIAlertController 并将 UIActivityIndi​​catorView 作为子视图添加到AlertView。

优势

  1. 实施简单,无需担心自动定位或 回转。 UIAlertController 将自动处理它。
  2. 默认情况下,用户互动将被自动禁用 AlertController中的行为

否则,如果您希望自定义控件使用现成的实用程序,而这些实用程序已经从CocoaPods中用于相同目的。