//
// ViewController.swift
// MyFirstApp
//
// Created by Divakar on 19/10/17.
// Copyright © 2017 Divakar. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
customActivityIndicatory(self.view, startAnimate: true)
//hide activityIndicatorView
// customActivityIndicatory(self.view, startAnimate: false)
}
@discardableResult
func customActivityIndicatory(_ viewContainer: UIView, startAnimate:Bool? = true) -> UIActivityIndicatorView {
let mainContainer: UIView = UIView(frame: viewContainer.frame)
mainContainer.center = viewContainer.center
mainContainer.backgroundColor = hexStringToUIColor(hex: "#FFFFFF")
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 = hexStringToUIColor(hex: "#444444")
viewBackgroundLoading.alpha = 0.5
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)
if startAnimate!{
viewBackgroundLoading.addSubview(activityIndicatorView)
mainContainer.addSubview(viewBackgroundLoading)
viewContainer.addSubview(mainContainer)
activityIndicatorView.startAnimating()
}else{
for subview in viewContainer.subviews{
if subview.tag == 789456123{
subview.removeFromSuperview()
}
}
}
return activityIndicatorView
}
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.characters.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}