Created
November 25, 2021 07:29
-
-
Save daryllukas/f93374f8032014806c7d0c20505d14ce to your computer and use it in GitHub Desktop.
Loading indicator for iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewControllerUtils.swift | |
// DesignMiami | |
// | |
// Created by Daryl Lukas on 11/24/21. | |
// | |
import Foundation | |
import UIKit | |
class ViewControllerUtils { | |
var container: UIView = UIView() | |
var loadingView: UIView = UIView() | |
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() | |
/* | |
Show customized activity indicator, | |
actually add activity indicator to passing view | |
@param uiView - add activity indicator to this view | |
*/ | |
func showActivityIndicator(uiView: UIView) { | |
container.frame = uiView.frame | |
container.center = uiView.center | |
container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.5) | |
loadingView.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0) | |
loadingView.center = uiView.center | |
loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7) | |
loadingView.clipsToBounds = true | |
loadingView.layer.cornerRadius = 10 | |
activityIndicator.frame = CGRect(x: 0, y: 0, width: 40.0, height: 40.0) | |
activityIndicator.style = .large | |
activityIndicator.color = .white | |
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() | |
} | |
/* | |
Hide activity indicator | |
Actually remove activity indicator from its super view | |
@param uiView - remove activity indicator from this view | |
*/ | |
func hideActivityIndicator(uiView: UIView) { | |
activityIndicator.stopAnimating() | |
activityIndicator.isHidden = true | |
container.removeFromSuperview() | |
} | |
/* | |
Define UIColor from hex value | |
@param rgbValue - hex color value | |
@param alpha - transparency level | |
*/ | |
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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment