ios - 在多个 View Controller 中使用相同的 UIActivityIndi​​catorView

标签 ios objective-c xcode

我有一个带有各种 View Controller 的简单 iOS 应用程序。 每个 View Controller 都有不同的功能,但每个 View Controller 都有“加载”按钮,当触发时,发送请求并获取结果给委托(delegate)方法。

我想使用一个 UIActivityIndi​​catorView,它会在用户单击按钮时启动,并会在委托(delegate)方法上停止。 显然,我希望指示器在每个 VC 上看起来都一样,所以我已经为它创建了属性,并且在每个 viewDidLoad 方法上我都使用以下代码:

self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
self.indicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
self.indicator.frame = CGRectMake(40.0, 20.0, 100.0, 100.0);
self.indicator.center = self.view.center;

问题是,我在同一个对象上使用相同的参数,将这些行复制并粘贴到每个 View Controller 上。 假设我想在下一个版本中更改样式,我需要更改 10 次。

使用某种静态指示器的最佳方法是什么,该指示器将使用这些参数进行设置并按需设置开启和关闭?

最佳答案

这是我在 swift 4.1 中使用的

import UIKit

class ProgressView {

    // MARK: - Variables
    private var containerView = UIView()
    private var progressView = UIView()
    private var activityIndicator = UIActivityIndicatorView()

    static var shared = ProgressView()

    // To close for instantiation
    private init() {}

    // MARK: - Functions
     func startAnimating(view: UIView = (UIApplication.shared.keyWindow?.rootViewController?.view)!) {
        containerView.center = view.center
        containerView.frame = view.frame
        containerView.backgroundColor = UIColor(hex: 0xffffff, alpha: 0.5)

        progressView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
        progressView.center = containerView.center
        progressView.backgroundColor = UIColor(hex: 0x444444, alpha: 0.7)
        progressView.clipsToBounds = true
        progressView.cornerRadius = 10

        activityIndicator.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
        activityIndicator.center = CGPoint(x: progressView.bounds.width/2, y: progressView.bounds.height/2)

        activityIndicator.style = .whiteLarge

        view.addSubview(containerView)
        containerView.addSubview(progressView)
        progressView.addSubview(activityIndicator)

        activityIndicator.startAnimating()
    }

    /// animate UIActivityIndicationView without blocking UI
    func startSmoothAnimation(view: UIView = (UIApplication.shared.keyWindow?.rootViewController?.view)!) {
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
        activityIndicator.center = view.center
        activityIndicator.style = .whiteLarge
        activityIndicator.color = UIColor.gray
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
    }

    func stopAnimatimating() {
        activityIndicator.stopAnimating()
        containerView.removeFromSuperview()
    }

}

extension UIColor {
    convenience init(hex: UInt32, alpha: CGFloat) {
        let red = CGFloat((hex & 0xFF0000) >> 16)/256.0
        let green = CGFloat((hex & 0xFF00) >> 8)/256.0
        let blue = CGFloat(hex & 0xFF)/256.0
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
}

// usage 
 ProgressView.shared.startAnimating()
// to stop 
 ProgressView.shared.stopAnimatimating()
Hope it helps

关于ios - 在多个 View Controller 中使用相同的 UIActivityIndi​​catorView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54516977/

相关文章:

ios - 如何在切换 ViewController 时保持背景音乐播放?

ios - 如何在 objective-c 中为此代码创建函数

ios - AWS Dynamodb测试使用swift 3报错

ios - 核心数据保存上下文

ios - 如何使推送通知在iOS的生产环境中工作?

objective-c - 为什么会有一个 NSInteger?

ios - Cocos2D 在 -(id) 初始化时崩溃

objective-c - 释放Objective-c中的返回对象错误

ios - 为整个 iOS 应用设置背景图片

ios - 中文字符而不是非拉丁字符(Mojibake 错误?!)