Skip to content

Commit

Permalink
add custom container
Browse files Browse the repository at this point in the history
  • Loading branch information
smolskyaleksey committed Jul 10, 2018
1 parent 4323478 commit e99980d
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 10 deletions.
8 changes: 8 additions & 0 deletions PKHUD.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
12010C8A1C3D0E8D005DC6FC /* PKHUDAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12010C891C3D0E8D005DC6FC /* PKHUDAnimation.swift */; };
12CE19F41E25784D0062D873 /* PKHUDDemoUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12CE19F31E25784D0062D873 /* PKHUDDemoUITests.swift */; };
12CE19FD1E2578680062D873 /* PKHUD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F996321E19514FD8001F73CA /* PKHUD.framework */; };
1DBF2D5820F4C61600B58B41 /* CustomContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DBF2D5720F4C61600B58B41 /* CustomContainerView.swift */; };
1DBF2D5A20F4C81100B58B41 /* CustomFrameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DBF2D5920F4C81100B58B41 /* CustomFrameView.swift */; };
3097AF3A1C4843AE007DD42B /* PKHUDRotatingImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3097AF391C4843AE007DD42B /* PKHUDRotatingImageView.swift */; };
640011571C5B6C080013F32B /* HUD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 640011551C5B6C080013F32B /* HUD.swift */; };
F908BEC01BB849290015E5A8 /* PKHUDSuccessView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F908BEBF1BB849290015E5A8 /* PKHUDSuccessView.swift */; };
Expand Down Expand Up @@ -86,6 +88,8 @@
12CE19F11E25784C0062D873 /* PKHUDDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PKHUDDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
12CE19F31E25784D0062D873 /* PKHUDDemoUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PKHUDDemoUITests.swift; sourceTree = "<group>"; };
12CE19F51E25784D0062D873 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1DBF2D5720F4C61600B58B41 /* CustomContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomContainerView.swift; sourceTree = "<group>"; };
1DBF2D5920F4C81100B58B41 /* CustomFrameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomFrameView.swift; sourceTree = "<group>"; };
3097AF391C4843AE007DD42B /* PKHUDRotatingImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKHUDRotatingImageView.swift; sourceTree = "<group>"; };
640011551C5B6C080013F32B /* HUD.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HUD.swift; sourceTree = "<group>"; };
F908BEBF1BB849290015E5A8 /* PKHUDSuccessView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKHUDSuccessView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -244,7 +248,9 @@
isa = PBXGroup;
children = (
F996324919514FEF001F73CA /* FrameView.swift */,
1DBF2D5920F4C81100B58B41 /* CustomFrameView.swift */,
F996324B19514FEF001F73CA /* Window.swift */,
1DBF2D5720F4C61600B58B41 /* CustomContainerView.swift */,
F996324C19514FEF001F73CA /* WindowRootViewController.swift */,
);
name = Internal;
Expand Down Expand Up @@ -442,11 +448,13 @@
F996325319514FEF001F73CA /* WindowRootViewController.swift in Sources */,
F908BEC31BB84D0B0015E5A8 /* PKHUDAnimating.swift in Sources */,
F908BEC81BB85EA70015E5A8 /* PKHUDProgressView.swift in Sources */,
1DBF2D5820F4C61600B58B41 /* CustomContainerView.swift in Sources */,
F908BEC01BB849290015E5A8 /* PKHUDSuccessView.swift in Sources */,
F908BEC51BB852C60015E5A8 /* PKHUDErrorView.swift in Sources */,
F935B15D1B2B8C20003C3734 /* PKHUDSquareBaseView.swift in Sources */,
F996325219514FEF001F73CA /* Window.swift in Sources */,
F935B15F1B2B8C7E003C3734 /* PKHUDWideBaseView.swift in Sources */,
1DBF2D5A20F4C81100B58B41 /* CustomFrameView.swift in Sources */,
F996324D19514FEF001F73CA /* PKHUDAssets.swift in Sources */,
F996324F19514FEF001F73CA /* PKHUD.swift in Sources */,
F935B1671B2B8D8C003C3734 /* PKHUDSystemActivityIndicatorView.swift in Sources */,
Expand Down
103 changes: 103 additions & 0 deletions PKHUD/CustomContainerView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// CustomContainerView.swift
// PKHUD
//
// Created by Smolsky Aleksey on 10.07.2018.
// Copyright © 2018 NSExceptional. All rights reserved.
//

import UIKit

/// The window used to display the PKHUD within. Placed atop the applications main window.
internal class CustomContainerView: UIView {

internal let frameView: CustomFrameView
internal init(frameView: CustomFrameView = CustomFrameView()) {
self.frameView = frameView
super.init(frame: CGRect.zero)
commonInit()
}

required init?(coder aDecoder: NSCoder) {
frameView = CustomFrameView()
super.init(coder: aDecoder)
commonInit()
}

fileprivate func commonInit() {
backgroundColor = UIColor.clear
isHidden = true

addSubview(backgroundView)
addSubview(frameView)
}

internal override func layoutSubviews() {
super.layoutSubviews()

frameView.center = center
backgroundView.frame = bounds
}

internal func showFrameView() {
layer.removeAllAnimations()
frameView.center = center
frameView.alpha = 1.0
isHidden = false
}

fileprivate var willHide = false

internal func hideFrameView(animated anim: Bool, completion: ((Bool) -> Void)? = nil) {
let finalize: (_ finished: Bool) -> Void = { finished in
self.isHidden = true
self.removeFromSuperview()
self.willHide = false

completion?(finished)
}

if isHidden {
return
}

willHide = true

if anim {
UIView.animate(withDuration: 0.8, animations: {
self.frameView.alpha = 0.0
self.hideBackground(animated: false)
}, completion: { _ in finalize(true) })
} else {
self.frameView.alpha = 0.0
finalize(true)
}
}

fileprivate let backgroundView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0.0, alpha: 0.25)
view.alpha = 0.0
return view
}()

internal func showBackground(animated anim: Bool) {
if anim {
UIView.animate(withDuration: 0.175, animations: {
self.backgroundView.alpha = 1.0
})
} else {
backgroundView.alpha = 1.0
}
}

internal func hideBackground(animated anim: Bool) {
if anim {
UIView.animate(withDuration: 0.65, animations: {
self.backgroundView.alpha = 0.0
})
} else {
backgroundView.alpha = 0.0
}
}
}
43 changes: 43 additions & 0 deletions PKHUD/CustomFrameView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// CustomFrameView.swift
// PKHUD
//
// Created by Smolsky Aleksey on 10.07.2018.
// Copyright © 2018 NSExceptional. All rights reserved.
//

import UIKit

/// Provides the general look and feel of the PKHUD, into which the eventual content is inserted.
internal class CustomFrameView: UIView {

override init(frame: CGRect = CGRect.zero) {
super.init(frame: frame)
commonInit()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}

fileprivate func commonInit() {
self.addSubview(self.content)
}

fileprivate var _content = UIView()
internal var content: UIView {
get {
return _content
}
set {
_content.removeFromSuperview()
_content = newValue
_content.clipsToBounds = true
_content.contentMode = .center
frame.size = _content.bounds.size
self.addSubview(_content)
}
}
}

11 changes: 1 addition & 10 deletions PKHUD/PKHUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open class PKHUD: NSObject {

public var viewToPresentOn: UIView?

fileprivate let container = ContainerView()
fileprivate let container = CustomContainerView()
fileprivate var hideTimer: Timer?

public typealias TimerAction = (Bool) -> Void
Expand Down Expand Up @@ -109,15 +109,6 @@ open class PKHUD: NSObject {
}
}

open var effect: UIVisualEffect? {
get {
return container.frameView.effect
}
set {
container.frameView.effect = newValue
}
}

open func show(onView view: UIView? = nil) {
let view: UIView = view ?? viewToPresentOn ?? UIApplication.shared.keyWindow!
if !view.subviews.contains(container) {
Expand Down

0 comments on commit e99980d

Please sign in to comment.