Created
January 22, 2016 06:58
-
-
Save rjjohnpatrickcruz/045889c67ae51e5b5450 to your computer and use it in GitHub Desktop.
Spinner for iOS Swift
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
// | |
// Spinner.swift | |
// | |
// Created by RJ John Patrick Cruz on 1/22/16. | |
// Copyright © 2016 RJ John Patrick Cruz. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class MyView : UIView { | |
override func drawRect(rect: CGRect) { | |
} | |
} | |
public class Spinner:UIView { | |
let utilities = Utilities() | |
public static let sharedInstance = Spinner() | |
public let activityIndicator = UIActivityIndicatorView() | |
public let viewContainer = UIView() | |
public var isVisible = false | |
private let kNavigationBarHeight:CGFloat = 44.0 | |
public class func show(canvas: UIView) { | |
self.sharedInstance.showActivityIndicator(canvas, true) | |
} | |
public class func show(canvas: UIView, _ hasNavigationBar: Bool) { | |
self.sharedInstance.showActivityIndicator(canvas, hasNavigationBar) | |
} | |
public class func hide() { | |
self.sharedInstance.hideActivityIndicator() | |
} | |
func showActivityIndicator(canvas: UIView, _ hasNavigationBar: Bool) { | |
self.isVisible = true | |
let window = UIApplication.sharedApplication().windows.first! | |
self.viewContainer.backgroundColor = utilities.getHexColor(0x737575).colorWithAlphaComponent(0.4) | |
self.viewContainer.frame = canvas.bounds | |
let superview = UIView(frame: CGRectMake(window.bounds.midX - 79, window.bounds.midY - 70, 0, 0)) | |
let shadowView = UIView(frame: CGRectMake(50, 50, 60, 60)) | |
shadowView.layer.shadowColor = UIColor.blackColor().CGColor | |
shadowView.layer.shadowOffset = CGSizeZero | |
shadowView.layer.shadowOpacity = 0.5 | |
shadowView.layer.shadowRadius = 5 | |
let view = MyView(frame: shadowView.bounds) | |
view.backgroundColor = UIColor.grayColor() | |
view.layer.cornerRadius = 10.0 | |
view.layer.borderColor = UIColor.grayColor().CGColor | |
view.layer.borderWidth = 0.5 | |
view.clipsToBounds = true | |
self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray | |
self.activityIndicator.center = CGPointMake(view.bounds.size.width / 2.0, (view.bounds.size.height / 2.0) - (hasNavigationBar ? kNavigationBarHeight : 0)); | |
self.activityIndicator.activityIndicatorViewStyle = .WhiteLarge | |
self.activityIndicator.startAnimating() | |
view.addSubview(self.activityIndicator) | |
shadowView.addSubview(view) | |
superview.addSubview(shadowView) | |
self.viewContainer.addSubview(superview) | |
canvas.addSubview(self.viewContainer) | |
} | |
func hideActivityIndicator() { | |
UIView.animateWithDuration(0.15, delay: 0, options: (UIViewAnimationOptions.CurveEaseIn), animations: { () -> Void in | |
self.alpha = 0 | |
}) { (finished) -> Void in | |
self.viewContainer.removeFromSuperview() | |
self.isVisible = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment