Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for macOS by using shims #137

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions PKHUD.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ Pod::Spec.new do |s|
s.name = 'PKHUD'
s.module_name = 'PKHUD'
s.version = '4.2.0'
s.summary = 'A Swift 3 based reimplementation of the Apple HUD (Volume, Ringer, Rotation,…) for iOS 8 and up'
s.summary = 'A Swift 3 based reimplementation of the Apple HUD (Volume, Ringer, Rotation,…) for iOS 8/macOS 10.11 and up'
s.homepage = 'https://github.com/pkluz/PKHUD'
s.license = 'MIT'
s.author = { 'Philip Kluz' => '[email protected]' }
s.platform = :ios, '8.0'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.11'
s.requires_arc = true
s.source = { :git => 'https://github.com/pkluz/PKHUD.git', :tag => s.version.to_s }
s.source_files = 'PKHUD/**/*.{h,swift}'
s.source_files = 'PKHUD/**/*.swift'
s.osx.source_files = 'PKHUDMACOS/**/*.swift'
s.resources = 'PKHUD/*.xcassets'
end
353 changes: 345 additions & 8 deletions PKHUD.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

37 changes: 25 additions & 12 deletions PKHUD/FrameView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@
// Licensed under the MIT license.
//

import UIKit
#if os(iOS) || os(watchOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

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

#if os(OSX)
override internal var isFlipped:Bool {
get {
return true
}
}
#endif

internal init() {
super.init(effect: UIBlurEffect(style: .light))
super.init(effect: BlurEffect(style: .light))
commonInit()
}

Expand All @@ -23,12 +35,13 @@ internal class FrameView: UIVisualEffectView {
}

fileprivate func commonInit() {
backgroundColor = UIColor(white: 0.8, alpha: 0.36)
layer.cornerRadius = 9.0
layer.masksToBounds = true

backgroundColor = Color(white: 0.8, alpha: 0.36)
self.cornerRadius = 9.0
self.masksToBounds = true
contentView.addSubview(self.content)


#if os(iOS) || os(watchOS)
let offset = 20.0

let motionEffectsX = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
Expand All @@ -43,10 +56,11 @@ internal class FrameView: UIVisualEffectView {
group.motionEffects = [motionEffectsX, motionEffectsY]

addMotionEffect(group)
#endif
}

fileprivate var _content = UIView()
internal var content: UIView {
fileprivate var _content = View()
internal var content: View {
get {
return _content
}
Expand All @@ -55,7 +69,6 @@ internal class FrameView: UIVisualEffectView {
_content = newValue
_content.alpha = 0.85
_content.clipsToBounds = true
_content.contentMode = .center
frame.size = _content.bounds.size
addSubview(_content)
}
Expand Down
34 changes: 20 additions & 14 deletions PKHUD/HUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@
// Licensed under the MIT license.
//

import UIKit
#if os(iOS) || os(watchOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

public enum HUDContentType {
case success
case error
case progress
case image(UIImage?)
case rotatingImage(UIImage?)

case image(Img?)
case rotatingImage(Img?)
case labeledSuccess(title: String?, subtitle: String?)
case labeledError(title: String?, subtitle: String?)
case labeledProgress(title: String?, subtitle: String?)
case labeledImage(image: UIImage?, title: String?, subtitle: String?)
case labeledRotatingImage(image: UIImage?, title: String?, subtitle: String?)

case labeledImage(image: Img?, title: String?, subtitle: String?)
case labeledRotatingImage(image: Img?, title: String?, subtitle: String?)
case label(String?)
case systemActivity
}
Expand All @@ -42,9 +46,11 @@ public final class HUD {
public static var isVisible: Bool { return PKHUD.sharedHUD.isVisible }

// MARK: Public methods, PKHUD based
public static func show(_ content: HUDContentType, onView view: UIView? = nil) {
PKHUD.sharedHUD.contentView = contentView(content)
PKHUD.sharedHUD.show(onView: view)
public static func show(_ content: HUDContentType, onView view: View? = nil) {
let hud = PKHUD.sharedHUD
hud.viewToPresentOn = view
hud.contentView = contentView(content)
hud.show()
}

public static func hide(_ completion: ((Bool) -> Void)? = nil) {
Expand All @@ -60,18 +66,18 @@ public final class HUD {
}

// MARK: Public methods, HUD based
public static func flash(_ content: HUDContentType, onView view: UIView? = nil) {
public static func flash(_ content: HUDContentType, onView view: View? = nil) {
HUD.show(content, onView: view)
HUD.hide(animated: true, completion: nil)
}

public static func flash(_ content: HUDContentType, onView view: UIView? = nil, delay: TimeInterval, completion: ((Bool) -> Void)? = nil) {
public static func flash(_ content: HUDContentType, onView view: View? = nil, delay: TimeInterval, completion: ((Bool) -> Void)? = nil) {
HUD.show(content, onView: view)
HUD.hide(afterDelay: delay, completion: completion)
}

// MARK: Private methods
fileprivate static func contentView(_ content: HUDContentType) -> UIView {
fileprivate static func contentView(_ content: HUDContentType) -> View {
switch content {
case .success:
return PKHUDSuccessView()
Expand Down
40 changes: 29 additions & 11 deletions PKHUD/PKHUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
// Licensed under the MIT license.
//

import UIKit
#if os(iOS) || os(watchOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

/// The PKHUD object controls showing and hiding of the HUD, as well as its contents and touch response behavior.
open class PKHUD: NSObject {

fileprivate struct Constants {
static let sharedHUD = PKHUD()
}

public var viewToPresentOn: UIView? = nil
public var viewToPresentOn: View?

fileprivate let container = ContainerView()
fileprivate var hideTimer: Timer?
Expand All @@ -40,21 +44,25 @@ open class PKHUD: NSObject {

public override init () {
super.init()
#if os(iOS) || os(watchOS)
NotificationCenter.default.addObserver(self,
selector: #selector(PKHUD.willEnterForeground(_:)),
name: NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
#endif
userInteractionOnUnderlyingViewsEnabled = false
container.frameView.autoresizingMask = [ .flexibleLeftMargin,
.flexibleRightMargin,
.flexibleTopMargin,
.flexibleBottomMargin ]

#if os(iOS) || os(watchOS)
self.container.isAccessibilityElement = true
self.container.accessibilityIdentifier = "PKHUD"
#endif
}

public convenience init(viewToPresentOn view: UIView) {
public convenience init(viewToPresentOn view: View?) {
self.init()
viewToPresentOn = view
}
Expand All @@ -76,8 +84,8 @@ open class PKHUD: NSObject {
open var isVisible: Bool {
return !container.isHidden
}

open var contentView: UIView {
open var contentView: View {
get {
return container.frameView.content
}
Expand All @@ -86,7 +94,8 @@ open class PKHUD: NSObject {
startAnimatingContentView()
}
}


#if os(iOS) || os(watchOS)
open var effect: UIVisualEffect? {
get {
return container.frameView.effect
Expand All @@ -95,9 +104,18 @@ open class PKHUD: NSObject {
container.frameView.effect = newValue
}
}

open func show(onView view: UIView? = nil) {
let view: UIView = view ?? viewToPresentOn ?? UIApplication.shared.keyWindow!
#endif

open func show() {
#if os(iOS) || os(watchOS)
guard let view = viewToPresentOn ?? UIApplication.shared.keyWindow ?? UIApplication.shared.windows.first else {
preconditionFailure("HUD has no view to present on")
}
#elseif os(OSX)
guard let view = viewToPresentOn ?? NSApplication.shared().orderedWindows.first?.contentView else {
preconditionFailure("HUD has no view to present on")
}
#endif
if !view.subviews.contains(container) {
view.addSubview(container)
container.frame.origin = CGPoint.zero
Expand Down
2 changes: 0 additions & 2 deletions PKHUD/PKHUDAnimating.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// Licensed under the MIT license.
//

import UIKit

@objc public protocol PKHUDAnimating {

func startAnimation()
Expand Down
24 changes: 16 additions & 8 deletions PKHUD/PKHUDAssets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@
// Licensed under the MIT license.
//

import UIKit
#if os(iOS) || os(watchOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

/// PKHUDAssets provides a set of default images, that can be supplied to the PKHUD's content views.
open class PKHUDAssets: NSObject {

open class var crossImage: UIImage { return PKHUDAssets.bundledImage(named: "cross") }
open class var checkmarkImage: UIImage { return PKHUDAssets.bundledImage(named: "checkmark") }
open class var progressActivityImage: UIImage { return PKHUDAssets.bundledImage(named: "progress_activity") }
open class var progressCircularImage: UIImage { return PKHUDAssets.bundledImage(named: "progress_circular") }
open class var crossImage: Img { return PKHUDAssets.bundledImage(named: "cross") }
open class var checkmarkImage: Img { return PKHUDAssets.bundledImage(named: "checkmark") }
open class var progressActivityImage: Img { return PKHUDAssets.bundledImage(named: "progress_activity") }
open class var progressCircularImage: Img { return PKHUDAssets.bundledImage(named: "progress_circular") }

internal class func bundledImage(named name: String) -> UIImage {
internal class func bundledImage(named name: String) -> Img {
let bundle = Bundle(for: PKHUDAssets.self)
let image = UIImage(named: name, in:bundle, compatibleWith:nil)
#if os(iOS) || os(watchOS)
let image = Img(named: name, in: bundle, compatibleWith: nil)
#elseif os(OSX)
let image = bundle.image(forResource: name)
#endif
if let image = image {
return image
}

return UIImage()
return Img()
}
}
26 changes: 15 additions & 11 deletions PKHUD/PKHUDErrorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
// Licensed under the MIT license.
//

import UIKit
#if os(iOS) || os(watchOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

/// PKHUDErrorView provides an animated error (cross) view.
open class PKHUDErrorView: PKHUDSquareBaseView, PKHUDAnimating {
Expand All @@ -19,34 +23,34 @@ open class PKHUDErrorView: PKHUDSquareBaseView, PKHUDAnimating {
let dash = CAShapeLayer()
dash.frame = CGRect(x: 0.0, y: 0.0, width: 88.0, height: 88.0)
dash.path = {
let path = UIBezierPath()
let path = BezierPath()
path.move(to: CGPoint(x: 0.0, y: 44.0))
path.addLine(to: CGPoint(x: 88.0, y: 44.0))
return path.cgPath
}()
dash.lineCap = kCALineCapRound
dash.lineJoin = kCALineJoinRound
dash.fillColor = nil
dash.strokeColor = UIColor(red: 0.15, green: 0.15, blue: 0.15, alpha: 1.0).cgColor
dash.strokeColor = Color(red: 0.15, green: 0.15, blue: 0.15, alpha: 1.0).cgColor
dash.lineWidth = 6
dash.fillMode = kCAFillModeForwards
return dash
}

public init(title: String? = nil, subtitle: String? = nil) {
super.init(title: title, subtitle: subtitle)
layer.addSublayer(dashOneLayer)
layer.addSublayer(dashTwoLayer)
dashOneLayer.position = layer.position
dashTwoLayer.position = layer.position
self.addSublayer(dashOneLayer)
self.addSublayer(dashTwoLayer)
dashOneLayer.position = self.center
dashTwoLayer.position = self.center
}

public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
layer.addSublayer(dashOneLayer)
layer.addSublayer(dashTwoLayer)
dashOneLayer.position = layer.position
dashTwoLayer.position = layer.position
self.addSublayer(dashOneLayer)
self.addSublayer(dashTwoLayer)
dashOneLayer.position = self.center
dashTwoLayer.position = self.center
}

func rotationAnimation(_ angle: CGFloat) -> CABasicAnimation {
Expand Down
11 changes: 8 additions & 3 deletions PKHUD/PKHUDProgressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
// Licensed under the MIT license.
//

import UIKit
#if os(iOS) || os(watchOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

import QuartzCore

/// PKHUDProgressView provides an indeterminate progress view.
Expand All @@ -20,9 +25,9 @@ open class PKHUDProgressView: PKHUDSquareBaseView, PKHUDAnimating {
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

public func startAnimation() {
imageView.layer.add(PKHUDAnimation.discreteRotation, forKey: "progressAnimation")
imageView.add(PKHUDAnimation.discreteRotation, forKey: "progressAnimation")
}

public func stopAnimation() {
Expand Down
Loading