Blame view
Pods/Toaster/Sources/ToastWindow.swift
4.78 KB
|
d774f0637
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
/*
* ToastView.swift
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2013-2015 Su Yeol Jeon
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
*/
import UIKit
open class ToastWindow: UIWindow {
open static let shared = ToastWindow(frame: UIScreen.main.bounds)
/// Will not return `rootViewController` while this value is `true`. Or the rotation will be fucked in iOS 9.
var isStatusBarOrientationChanging = false
/// Don't rotate manually if the application:
///
/// - is running on iPad
/// - is running on iOS 9
/// - supports all orientations
/// - doesn't require full screen
/// - has launch storyboard
///
var shouldRotateManually: Bool {
let iPad = UIDevice.current.userInterfaceIdiom == .pad
let application = UIApplication.shared
let window = application.delegate?.window ?? nil
let supportsAllOrientations = application.supportedInterfaceOrientations(for: window) == .all
let info = Bundle.main.infoDictionary
let requiresFullScreen = (info?["UIRequiresFullScreen"] as? NSNumber)?.boolValue == true
let hasLaunchStoryboard = info?["UILaunchStoryboardName"] != nil
if #available(iOS 9, *), iPad && supportsAllOrientations && !requiresFullScreen && hasLaunchStoryboard {
return false
}
return true
}
override open var rootViewController: UIViewController? {
get {
guard !self.isStatusBarOrientationChanging else { return nil }
guard let firstWindow = UIApplication.shared.windows.first else { return nil }
return firstWindow is ToastWindow ? nil : firstWindow.rootViewController
}
set { /* Do nothing */ }
}
public override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = false
self.windowLevel = CGFloat.greatestFiniteMagnitude
self.backgroundColor = .clear
self.isHidden = false
self.handleRotate(UIApplication.shared.statusBarOrientation)
NotificationCenter.default.addObserver(
self,
selector: #selector(self.bringWindowToTop),
name: .UIWindowDidBecomeVisible,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(self.statusBarOrientationWillChange),
name: .UIApplicationWillChangeStatusBarOrientation,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(self.statusBarOrientationDidChange),
name: .UIApplicationDidChangeStatusBarOrientation,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(self.applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive,
object: nil
)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// Bring ToastWindow to top when another window is being shown.
func bringWindowToTop(_ notification: Notification) {
if !(notification.object is ToastWindow) {
ToastWindow.shared.isHidden = true
ToastWindow.shared.isHidden = false
}
}
dynamic func statusBarOrientationWillChange() {
self.isStatusBarOrientationChanging = true
}
dynamic func statusBarOrientationDidChange() {
let orientation = UIApplication.shared.statusBarOrientation
self.handleRotate(orientation)
self.isStatusBarOrientationChanging = false
}
func applicationDidBecomeActive() {
let orientation = UIApplication.shared.statusBarOrientation
self.handleRotate(orientation)
}
func handleRotate(_ orientation: UIInterfaceOrientation) {
let angle = self.angleForOrientation(orientation)
if self.shouldRotateManually {
self.transform = CGAffineTransform(rotationAngle: CGFloat(angle))
}
if let window = UIApplication.shared.windows.first {
if orientation.isPortrait || !self.shouldRotateManually {
self.frame.size.width = window.bounds.size.width
self.frame.size.height = window.bounds.size.height
} else {
self.frame.size.width = window.bounds.size.height
self.frame.size.height = window.bounds.size.width
}
}
self.frame.origin = .zero
DispatchQueue.main.async {
ToastCenter.default.currentToast?.view.setNeedsLayout()
}
}
func angleForOrientation(_ orientation: UIInterfaceOrientation) -> Double {
switch orientation {
case .landscapeLeft: return -.pi / 2
case .landscapeRight: return .pi / 2
case .portraitUpsideDown: return .pi
default: return 0
}
}
}
|