korhan-Ö
11/5/2016 - 8:31 AM

#Loading Indicator

let indicator = T2_showLoadingAnimation(self.view)

T2_dismissLoadingAnimation(indicator.0, container: indicator.1)


func T2_showLoadingAnimation(uiView: UIView) -> (UIActivityIndicatorView, UIView) {
    let container: UIView = UIView()
    container.frame = uiView.frame
    container.center = uiView.center
    container.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
    
    let loadingView: UIView = UIView()
    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = uiView.center
    loadingView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10
    
    let 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()
    
    return (actInd,container)
}

func T2_dismissLoadingAnimation (actInd:UIActivityIndicatorView, container:UIView) {
    
    
    container.removeFromSuperview()
    actInd.stopAnimating()
    
    
}
let indicator = self.funcs.showLoadingAnimation(view)

//dismiss
self.funcs.dismissLoadingAnimation(indicator.0, viewActivityIndicator: indicator.1, blurEffectView: indicator.2)

func showLoadingAnimation(view:UIView) -> ( UIActivityIndicatorView, UIView, UIVisualEffectView) {
        
        var activityIndicator: UIActivityIndicatorView!
        
        var viewActivityIndicator: UIView!
        
        
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        
        
        blurEffectView.frame = view.bounds
        blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // for supporting device rotation
        
        blurEffectView.alpha = 0.99
        view.addSubview(blurEffectView)
        
        
        
        let width: CGFloat = 200.0
        let height: CGFloat = 50.0
        let x = view.frame.width/2.0 - width/2.0
        let y = view.frame.height/2.0 - height/2.0
        
        viewActivityIndicator = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
        viewActivityIndicator.backgroundColor = UIColor(red: 255.0/255.0, green: 204.0/255.0, blue: 51.0/255.0, alpha: 0.5)
        viewActivityIndicator.layer.cornerRadius = 10
        
        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        activityIndicator.color = UIColor.blackColor()
        activityIndicator.hidesWhenStopped = false
        
        let titleLabel = UILabel(frame: CGRect(x: 60, y: 0, width: 200, height: 50))
        titleLabel.text = "Yükleniyor..."
        
        viewActivityIndicator.addSubview(activityIndicator)
        viewActivityIndicator.addSubview(titleLabel)
        
        view.addSubview(viewActivityIndicator)
        
        activityIndicator.startAnimating()
        
        UIApplication.sharedApplication().beginIgnoringInteractionEvents()
        
        return (activityIndicator, viewActivityIndicator, blurEffectView)
        
    }
    
    func dismissLoadingAnimation (activityIndicator:UIActivityIndicatorView, viewActivityIndicator:UIView, blurEffectView:UIVisualEffectView) {
        activityIndicator.stopAnimating()
        UIApplication.sharedApplication().endIgnoringInteractionEvents()
        viewActivityIndicator.removeFromSuperview()
        blurEffectView.removeFromSuperview()
    }