Activity Indicator

//  Created by Atakan Cengiz KURT
import Foundation
import UIKit

open class ActivityIndicator {
    var timer = Timer()
    var second = 10
    var container = UIView()
 
    @objc func updateTimer(){
        second -= 1
        if second == 0 {
            timer.invalidate()
            customActivityIndicatory(container, startAnimate: false)
        }
    }
 
 

    func customActivityIndicatory(_ viewContainer: UIView, startAnimate:Bool? = true) -> UIActivityIndicatorView {
        let mainContainer: UIView = UIView(frame: viewContainer.frame)
        mainContainer.center = viewContainer.center
        mainContainer.backgroundColor = UIColor(hex: 0xFFFFFF, alpha: 0.7)
     
        mainContainer.alpha = 0.5
        mainContainer.tag = 789456123
        mainContainer.isUserInteractionEnabled = false
     
        let viewBackgroundLoading: UIView = UIView(frame: CGRect(x:0,y: 0,width: 80,height: 80))
        viewBackgroundLoading.center = viewContainer.center
        viewBackgroundLoading.backgroundColor = UIColor(hex: 0x444444, alpha: 1 )
        viewBackgroundLoading.alpha = 0.7
        viewBackgroundLoading.clipsToBounds = true
        viewBackgroundLoading.layer.cornerRadius = 15
     
        let activityIndicatorView: UIActivityIndicatorView = UIActivityIndicatorView()
        activityIndicatorView.frame = CGRect(x:0.0,y: 0.0,width: 40.0, height: 40.0)
        activityIndicatorView.activityIndicatorViewStyle =
            UIActivityIndicatorViewStyle.whiteLarge
        activityIndicatorView.center = CGPoint(x: viewBackgroundLoading.frame.size.width / 2, y: viewBackgroundLoading.frame.size.height / 2)
     
        container = viewContainer
     
        if startAnimate!{
            viewBackgroundLoading.addSubview(activityIndicatorView)
            mainContainer.addSubview(viewBackgroundLoading)
            viewContainer.addSubview(mainContainer)
            activityIndicatorView.startAnimating()
            UIApplication.shared.beginIgnoringInteractionEvents()
         
            timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
         
        }else{
            for subview in viewContainer.subviews{
                if subview.tag == 789456123{
                    subview.removeFromSuperview()
                }
            }
            UIApplication.shared.endIgnoringInteractionEvents()
        }
        return activityIndicatorView
    }
}

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

Controller'da kullanılacak kodlar;
//ActivityIndicator().customActivityIndicatory(self.view, startAnimate: true)  //ActivityIndicator başlat.
//ActivityIndicator().customActivityIndicatory(self.view, startAnimate: false) //ActivityIndicator durdur.


Karşılaştığım sorun; globalde her view'de kullanabileceğim hata durumunda bile kendini sonlandıracak ActivityIndicator yaratmak. Hata olması durumunda ActivityIndicator 10sn. geri sayımdan sonra sonlanacak şekilde düzenlendi.