diff --git a/swift/ViewControllerUtilsSwift3.swift b/swift/ViewControllerUtilsSwift3.swift new file mode 100644 index 0000000..ce26863 --- /dev/null +++ b/swift/ViewControllerUtilsSwift3.swift @@ -0,0 +1,54 @@ +// +// ActivityIndicator.swift +// My Smart List +// +// Created by Hakan on 21/06/2017. +// Copyright © 2017 HSmart Tech. All rights reserved. +// +import Foundation +import UIKit + +class ViewControllerUtils { + + var container: UIView = UIView() + var loadingView: UIView = UIView() + var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() + + func showActivityIndicator(uiView: UIView) { + container.frame = uiView.frame + container.center = uiView.center + container.backgroundColor = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3) + + loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80) + loadingView.center = uiView.center + loadingView.backgroundColor = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7) + loadingView.clipsToBounds = true + loadingView.layer.cornerRadius = 10 + + activityIndicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40) + activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge + activityIndicator.center = CGPoint(x: loadingView.frame.size.width / 2, y:loadingView.frame.size.height / 2) + + + loadingView.addSubview(activityIndicator) + container.addSubview(loadingView) + uiView.addSubview(container) + activityIndicator.startAnimating() + } + + + func hideActivityIndicator(uiView: UIView) { + activityIndicator.stopAnimating() + container.removeFromSuperview() + } + + func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor { + let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0 + let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0 + let blue = CGFloat(rgbValue & 0xFF)/256.0 + return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha)) + } + +} +// To use it add this file to your project and then first create class -> let v_c:ViewControllerUtils = ViewControllerUtils() +// then call it to show or hide -> v_c.showActivityIndicator(uiView: self.view) or v_c.hideActivityIndicator(uiView: self.view)