我使用Swift2和Xcode 7.1 我想通过调用函数来显示我的视图加载。我的观点chragement是一个继承自UIView的类。 我希望我的类在我的Controller中自动实例化。
我的课程:
class loading: UIView {
let circle = UIView()
let anim = CAKeyframeAnimation(keyPath: "position")
init() { }
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func clear(){
self.hidden = true
// Animation ...
}
func display(){
self.hidden = false
// Animation ...
}
private func scaleAndColor(){ }
private func createAnim(){ }
private func createCircle(frame: CGRect){ }
}
我想要的是什么:
class ViewController: UIViewController {
// nothing to declare
override func viewDidLoad() {
super.viewDidLoad()
// nothing to do
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func anFunction(){
load() // display my loadView
clear() // clear my loadView
}
}
答案 0 :(得分:1)
这是我在Swift 2.1中构建的Loader Singleton
import Foundation
import UIKit
class MGSwiftLoader: UIView {
static let sharedInstance = MGSwiftLoader(frame: CGRectZero)
private var backgroundView: UIView!
private var label: UILabel!
private let activityIndicator = UIActivityIndicatorView()
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
//See through view
self.backgroundColor = UIColor.clearColor()
self.opaque = false
//BackgroundView will contain the UIVisualEffectView, label and activityIndicator
backgroundView = UIView()
backgroundView.backgroundColor = UIColor.clearColor()
backgroundView.opaque = false
backgroundView.layer.cornerRadius = 5.0
self.addSubview(backgroundView)
backgroundView.snp_makeConstraints { (make) -> Void in
make.center.equalTo(self)
make.height.equalTo(100.0)
}
//Blur
let blurEffect = UIBlurEffect(style: .Light)
let blurredEffectView = UIVisualEffectView(effect: blurEffect)
blurredEffectView.layer.cornerRadius = 5.0
blurredEffectView.clipsToBounds = true
backgroundView.addSubview(blurredEffectView)
blurredEffectView.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(backgroundView)
}
//ActivityIndicator
activityIndicator.color = UIColor.lightGrayColor()
backgroundView.addSubview(activityIndicator)
activityIndicator.transform = CGAffineTransformMakeScale(1.15, 1.15)
activityIndicator.snp_makeConstraints { (make) -> Void in
make.center.equalTo(backgroundView)
}
}
//Shows the loder - The view takes all the screen disabling touches but we only see the backgroundView with its subviews
func show() {
self.hidden = false
//Take all of the MainWindow screen
let application = UIApplication.sharedApplication()
let frontWindow = application.keyWindow
frontWindow?.addSubview(self)
self.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(frontWindow!)
}
activityIndicator.startAnimating()
//Show top activity indicator
application.networkActivityIndicatorVisible = true
//Scale animation - you can add any animation you want
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = NSNumber(float: 0.0)
scaleAnimation.toValue = NSNumber(float: 1.0)
scaleAnimation.duration = 0.33
scaleAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
self.layer.addAnimation(scaleAnimation, forKey: "scaleAnimation")
}
func showWithSubTitle(text: String) {
show()
if label != nil {
label.removeFromSuperview()
}
activityIndicator.snp_remakeConstraints { (make) -> Void in
make.centerX.equalTo(backgroundView)
make.centerY.equalTo(backgroundView).offset(-10.0)
}
label = UILabel()
label.textColor = UIColor.lightGrayColor()
label.textAlignment = NSTextAlignment.Center
label.text = text
label.customFont(.Light, size: 15.0)
backgroundView.addSubview(label)
label.snp_makeConstraints { (make) -> Void in
make.bottom.equalTo(backgroundView).offset(-10.0)
make.right.equalTo(backgroundView).offset(-20.0)
make.left.equalTo(backgroundView).offset(20.0)
}
}
func hide() {
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = NSNumber(float: 1.0)
scaleAnimation.toValue = NSNumber(float: 0.0)
scaleAnimation.duration = 0.33
scaleAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
scaleAnimation.delegate = self
scaleAnimation.fillMode = kCAFillModeForwards
scaleAnimation.removedOnCompletion = false
self.layer.addAnimation(scaleAnimation, forKey: "scaleAnimation")
self.hidden = true
//Hide top activity indicator
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
您只需致电:
即可使用它MGSwiftLoader.sharedInstance.show()
或
MGSwiftLoader.sharedInstance.showWithSubTitle("Your Loader Text")
隐藏它:
MGSwiftLoader.sharedInstance.hide()
请记住,我正在使用SnapKit手动构建约束(http://snapkit.io)