Skip to content

Commit

Permalink
DemoCustomHUD: Add CustomLoading2
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hu committed Feb 6, 2017
1 parent 4abb017 commit 37a327a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
5 changes: 5 additions & 0 deletions DemoCustomHUD/DemoCustomHUD.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
A47EAB591E1E1A9900D25169 /* 6.png in Resources */ = {isa = PBXBuildFile; fileRef = A47EAB521E1E1A9900D25169 /* 6.png */; };
A47EAB5C1E1E1C9500D25169 /* CustomLoading.swift in Sources */ = {isa = PBXBuildFile; fileRef = A47EAB5B1E1E1C9500D25169 /* CustomLoading.swift */; };
A47EAB5E1E1E219B00D25169 /* DTSUIColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A47EAB5D1E1E219B00D25169 /* DTSUIColorExtension.swift */; };
A4E9F3691E484A9900818627 /* CustomLoading2.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4E9F3681E484A9900818627 /* CustomLoading2.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -38,6 +39,7 @@
A47EAB521E1E1A9900D25169 /* 6.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = 6.png; sourceTree = "<group>"; };
A47EAB5B1E1E1C9500D25169 /* CustomLoading.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomLoading.swift; sourceTree = "<group>"; };
A47EAB5D1E1E219B00D25169 /* DTSUIColorExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DTSUIColorExtension.swift; sourceTree = "<group>"; };
A4E9F3681E484A9900818627 /* CustomLoading2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomLoading2.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -92,6 +94,7 @@
A47EAB511E1E1A9900D25169 /* 5.png */,
A47EAB521E1E1A9900D25169 /* 6.png */,
A47EAB5B1E1E1C9500D25169 /* CustomLoading.swift */,
A4E9F3681E484A9900818627 /* CustomLoading2.swift */,
);
path = Loading;
sourceTree = "<group>";
Expand Down Expand Up @@ -179,6 +182,7 @@
A47EAB5E1E1E219B00D25169 /* DTSUIColorExtension.swift in Sources */,
A47EAB3B1E1E1A4B00D25169 /* AppDelegate.swift in Sources */,
A47EAB5C1E1E1C9500D25169 /* CustomLoading.swift in Sources */,
A4E9F3691E484A9900818627 /* CustomLoading2.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -340,6 +344,7 @@
A47EAB4B1E1E1A4B00D25169 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
69 changes: 69 additions & 0 deletions DemoCustomHUD/DemoCustomHUD/Loading/CustomLoading2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// CustomLoading2.swift
// DemoCustomHUD
//
// Created by Chris Hu on 17/2/6.
// Copyright © 2017年 com.icetime17. All rights reserved.
//

import UIKit
import Foundation

class CustomLoading2 {

private static var imageView: UIImageView!
private static var maskView: UIView!

private static var displayLink: CADisplayLink!

private static var rotationAngle: CGFloat = 0.0

class func start() {
guard let image = UIImage(named: "1.png") else { return }

if maskView == nil {
guard let keyWindow = UIApplication.shared.keyWindow else { return }
DispatchQueue.main.async {
maskView = UIView(frame: keyWindow.bounds)
maskView.backgroundColor = UIColor(hexString: 0x0, alpha: 0.4)
keyWindow.addSubview(maskView)
}
}

if imageView == nil {
DispatchQueue.main.async {
imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: image.size.width / 2, height: image.size.height / 2))
imageView.image = image
self.maskView.addSubview(self.imageView)
self.imageView.center = self.maskView.center
}
}

if displayLink == nil {
displayLink = CADisplayLink(target: self, selector: #selector(updateAction))
displayLink.add(to: RunLoop.current, forMode: .commonModes)
}

UIApplication.shared.beginIgnoringInteractionEvents()
}

@objc private class func updateAction() {
rotationAngle += 0.075
let transform = CATransform3DMakeRotation(rotationAngle, 0, 1, 0)
imageView.layer.transform = transform
}

class func stop() {
displayLink.invalidate()
displayLink = nil
rotationAngle = 0

imageView.removeFromSuperview()
imageView = nil
maskView.removeFromSuperview()
maskView = nil

UIApplication.shared.endIgnoringInteractionEvents()
}

}
15 changes: 11 additions & 4 deletions DemoCustomHUD/DemoCustomHUD/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ import UIKit

class ViewController: UIViewController {

var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
imageView.image = UIImage(named: "1.png")
imageView.center = view.center
view.addSubview(imageView)

let btnTest = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
btnTest.center = view.center
btnTest.center = CGPoint(x: imageView.center.x, y: imageView.center.y + imageView.frame.height/2 + 100)
btnTest.backgroundColor = UIColor.gray
btnTest.setTitleColor(UIColor.white, for: .normal)
btnTest.setTitle("Loading", for: .normal)
Expand All @@ -24,10 +31,10 @@ class ViewController: UIViewController {
}

func actionBtnTest() {
CustomLoading.start()
CustomLoading2.start()

DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
CustomLoading.stop()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
CustomLoading2.stop()
}
}

Expand Down

0 comments on commit 37a327a

Please sign in to comment.