diff --git a/.swift-version b/.swift-version index 9f55b2c..8c50098 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -3.0 +3.1 diff --git a/AlertBar.podspec b/AlertBar.podspec index 0f1ea1b..9c098d4 100644 --- a/AlertBar.podspec +++ b/AlertBar.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "AlertBar" - s.version = "0.3.1" + s.version = "0.3.2" s.summary = "An easy alert on status bar." s.homepage = "https://github.com/jinSasaki/AlertBar" s.screenshots = "https://github.com/jinSasaki/AlertBar/raw/master/assets/demo.gif" diff --git a/AlertBar.xcodeproj/project.pbxproj b/AlertBar.xcodeproj/project.pbxproj index 8d3c33a..0c22169 100644 --- a/AlertBar.xcodeproj/project.pbxproj +++ b/AlertBar.xcodeproj/project.pbxproj @@ -10,7 +10,7 @@ 93A4C4761E223B0B00C0962F /* AlertBar.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 93A4C46C1E223B0B00C0962F /* AlertBar.framework */; }; 93A4C47B1E223B0B00C0962F /* AlertBarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93A4C47A1E223B0B00C0962F /* AlertBarTests.swift */; }; 93A4C47D1E223B0B00C0962F /* AlertBar.h in Headers */ = {isa = PBXBuildFile; fileRef = 93A4C46F1E223B0B00C0962F /* AlertBar.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 93A4C4A91E223DD700C0962F /* AlertBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93A4C4A81E223DD700C0962F /* AlertBar.swift */; }; + 93C0F48D1E8AC52800F15353 /* AlertBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93C0F48C1E8AC52800F15353 /* AlertBar.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -30,7 +30,7 @@ 93A4C4751E223B0B00C0962F /* AlertBarTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AlertBarTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 93A4C47A1E223B0B00C0962F /* AlertBarTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertBarTests.swift; sourceTree = ""; }; 93A4C47C1E223B0B00C0962F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 93A4C4A81E223DD700C0962F /* AlertBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertBar.swift; sourceTree = ""; }; + 93C0F48C1E8AC52800F15353 /* AlertBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertBar.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -74,7 +74,7 @@ isa = PBXGroup; children = ( 93A4C46F1E223B0B00C0962F /* AlertBar.h */, - 93A4C4A71E223DD700C0962F /* Sources */, + 93C0F48B1E8AC52800F15353 /* Sources */, 93A4C4701E223B0B00C0962F /* Info.plist */, ); path = AlertBar; @@ -89,13 +89,13 @@ path = AlertBarTests; sourceTree = ""; }; - 93A4C4A71E223DD700C0962F /* Sources */ = { + 93C0F48B1E8AC52800F15353 /* Sources */ = { isa = PBXGroup; children = ( - 93A4C4A81E223DD700C0962F /* AlertBar.swift */, + 93C0F48C1E8AC52800F15353 /* AlertBar.swift */, ); path = Sources; - sourceTree = ""; + sourceTree = SOURCE_ROOT; }; /* End PBXGroup section */ @@ -208,7 +208,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 93A4C4A91E223DD700C0962F /* AlertBar.swift in Sources */, + 93C0F48D1E8AC52800F15353 /* AlertBar.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/AlertBar/Sources/AlertBar.swift b/AlertBar/Sources/AlertBar.swift deleted file mode 100644 index 0ebe226..0000000 --- a/AlertBar/Sources/AlertBar.swift +++ /dev/null @@ -1,149 +0,0 @@ -// -// AlertBar.swift -// -// Created by Jin Sasaki on 2016/01/01. -// Copyright © 2016年 Jin Sasaki. All rights reserved. -// - -import UIKit - -public enum AlertBarType { - case success - case error - case notice - case warning - case info - case custom(UIColor, UIColor) - - var backgroundColor: UIColor { - get { - switch self { - case .success: - return UIColor(0x4CAF50) - case .error: - return UIColor(0xf44336) - case .notice: - return UIColor(0x2196F3) - case .warning: - return UIColor(0xFFC107) - case .info: - return UIColor(0x009688) - case .custom(let backgroundColor, _): - return backgroundColor - } - } - } - var textColor: UIColor { - get { - switch self { - case .custom(_, let textColor): - return textColor - default: - return UIColor(0xFFFFFF) - } - } - } -} - -open class AlertBar: UIView { - open static var textAlignment: NSTextAlignment = .left - static var alertBars: [AlertBar] = [] - - let messageLabel = UILabel() - - required public init?(coder aDecoder: NSCoder) { - fatalError("NSCoding not supported") - } - - override init(frame: CGRect) { - super.init(frame: frame) - messageLabel.frame = CGRect(x: 2, y: 2, width: frame.width - 4, height: frame.height - 4) - messageLabel.font = UIFont.systemFont(ofSize: 12) - self.addSubview(messageLabel) - - NotificationCenter.default.addObserver(self, selector: #selector(self.handleRotate(_:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) - } - - deinit { - NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) - } - - dynamic fileprivate func handleRotate(_ notification: Notification) { - self.removeFromSuperview() - AlertBar.alertBars = [] - } - - open class func show(_ type: AlertBarType, message: String, duration: Double = 2, completion: (() -> Void)? = nil) { - let statusBarHeight = UIApplication.shared.statusBarFrame.height - let alertBar = AlertBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: statusBarHeight)) - alertBar.messageLabel.text = message - alertBar.messageLabel.textAlignment = AlertBar.textAlignment - alertBar.backgroundColor = type.backgroundColor - alertBar.messageLabel.textColor = type.textColor - AlertBar.alertBars.append(alertBar) - - let width = UIScreen.main.bounds.width - let height = UIScreen.main.bounds.height - - let baseView = UIView(frame: UIScreen.main.bounds) - baseView.isUserInteractionEnabled = false - baseView.addSubview(alertBar) - - let window: UIWindow - let orientation = UIApplication.shared.statusBarOrientation - if orientation.isLandscape { - window = UIWindow(frame: CGRect(x: 0, y: 0, width: height, height: width)) - let sign: CGFloat = orientation == .landscapeLeft ? -1 : 1 - let d = fabs(width - height) / 2 - baseView.transform = CGAffineTransform(rotationAngle: sign * CGFloat.pi / 2).translatedBy(x: sign * d, y: sign * d) - } else { - window = UIWindow(frame: CGRect(x: 0, y: 0, width: width, height: height)) - if orientation == .portraitUpsideDown { - baseView.transform = CGAffineTransform(rotationAngle: CGFloat.pi) - } - } - window.isUserInteractionEnabled = false - window.windowLevel = UIWindowLevelStatusBar + 1 + CGFloat(AlertBar.alertBars.count) - window.addSubview(baseView) - window.makeKeyAndVisible() - - alertBar.transform = CGAffineTransform(translationX: 0, y: -statusBarHeight) - UIView.animate(withDuration: 0.2, - animations: { () -> Void in - alertBar.transform = CGAffineTransform.identity - }, completion: { _ in - UIView.animate(withDuration: 0.2, - delay: duration, - options: UIViewAnimationOptions(), - animations: { () -> Void in - alertBar.transform = CGAffineTransform(translationX: 0, y: -statusBarHeight) - }, - completion: { (animated: Bool) -> Void in - alertBar.removeFromSuperview() - if let index = AlertBar.alertBars.index(of: alertBar) { - AlertBar.alertBars.remove(at: index) - } - // To hold window instance - window.isHidden = true - completion?() - }) - }) - } - - open class func show(error: Error, duration: Double = 2, completion: (() -> Void)? = nil) { - let code = (error as NSError).code - let localizedDescription = error.localizedDescription - self.show(.error, message: "(\(code)) " + localizedDescription, duration: duration, completion: completion) - } -} - -internal extension UIColor { - convenience init(_ rgbValue: UInt) { - self.init( - red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, - green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, - blue: CGFloat(rgbValue & 0x0000FF) / 255.0, - alpha: CGFloat(1.0) - ) - } -} diff --git a/Example/AlertBarExample.xcodeproj/project.pbxproj b/Example/AlertBarExample.xcodeproj/project.pbxproj index b1d743a..61dfd8b 100644 --- a/Example/AlertBarExample.xcodeproj/project.pbxproj +++ b/Example/AlertBarExample.xcodeproj/project.pbxproj @@ -12,7 +12,7 @@ 93A4C4981E223C7B00C0962F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 93A4C4961E223C7B00C0962F /* Main.storyboard */; }; 93A4C49A1E223C7B00C0962F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 93A4C4991E223C7B00C0962F /* Assets.xcassets */; }; 93A4C49D1E223C7B00C0962F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 93A4C49B1E223C7B00C0962F /* LaunchScreen.storyboard */; }; - 93A4C4A61E223CDB00C0962F /* AlertBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93A4C4A51E223CDB00C0962F /* AlertBar.swift */; }; + 93C0F48A1E8AC48500F15353 /* AlertBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93C0F4891E8AC48500F15353 /* AlertBar.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -23,7 +23,7 @@ 93A4C4991E223C7B00C0962F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 93A4C49C1E223C7B00C0962F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 93A4C49E1E223C7B00C0962F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 93A4C4A51E223CDB00C0962F /* AlertBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlertBar.swift; sourceTree = ""; }; + 93C0F4891E8AC48500F15353 /* AlertBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AlertBar.swift; path = ../../../AlertBar/Sources/AlertBar.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -70,7 +70,7 @@ 93A4C4A41E223CDB00C0962F /* Sources */ = { isa = PBXGroup; children = ( - 93A4C4A51E223CDB00C0962F /* AlertBar.swift */, + 93C0F4891E8AC48500F15353 /* AlertBar.swift */, ); path = Sources; sourceTree = ""; @@ -148,7 +148,7 @@ buildActionMask = 2147483647; files = ( 93A4C4951E223C7B00C0962F /* ViewController.swift in Sources */, - 93A4C4A61E223CDB00C0962F /* AlertBar.swift in Sources */, + 93C0F48A1E8AC48500F15353 /* AlertBar.swift in Sources */, 93A4C4931E223C7B00C0962F /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Example/AlertBarExample/Sources/AlertBar.swift b/Example/AlertBarExample/Sources/AlertBar.swift deleted file mode 100644 index 8e4ffe9..0000000 --- a/Example/AlertBarExample/Sources/AlertBar.swift +++ /dev/null @@ -1,149 +0,0 @@ -// -// AlertBar.swift -// -// Created by Jin Sasaki on 2016/01/01. -// Copyright © 2016年 Jin Sasaki. All rights reserved. -// - -import UIKit - -public enum AlertBarType { - case success - case error - case notice - case warning - case info - case custom(UIColor, UIColor) - - var backgroundColor: UIColor { - get { - switch self { - case .success: - return UIColor(0x4CAF50) - case .error: - return UIColor(0xf44336) - case .notice: - return UIColor(0x2196F3) - case .warning: - return UIColor(0xFFC107) - case .info: - return UIColor(0x009688) - case .custom(let backgroundColor, _): - return backgroundColor - } - } - } - var textColor: UIColor { - get { - switch self { - case .custom(_, let textColor): - return textColor - default: - return UIColor(0xFFFFFF) - } - } - } -} - -open class AlertBar: UIView { - open static var textAlignment: NSTextAlignment = .left - static var alertBars: [AlertBar] = [] - - let messageLabel = UILabel() - - required public init?(coder aDecoder: NSCoder) { - fatalError("NSCoding not supported") - } - - override init(frame: CGRect) { - super.init(frame: frame) - messageLabel.frame = CGRect(x: 2, y: 2, width: frame.width - 4, height: frame.height - 4) - messageLabel.font = UIFont.systemFont(ofSize: 12) - self.addSubview(messageLabel) - - NotificationCenter.default.addObserver(self, selector: #selector(self.handleRotate(_:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) - } - - deinit { - NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) - } - - dynamic fileprivate func handleRotate(_ notification: Notification) { - self.removeFromSuperview() - AlertBar.alertBars = [] - } - - open class func show(_ type: AlertBarType, message: String, duration: Double = 2, completion: (() -> Void)? = nil) { - let statusBarHeight = UIApplication.shared.statusBarFrame.height - let alertBar = AlertBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: statusBarHeight)) - alertBar.messageLabel.text = message - alertBar.messageLabel.textAlignment = AlertBar.textAlignment - alertBar.backgroundColor = type.backgroundColor - alertBar.messageLabel.textColor = type.textColor - AlertBar.alertBars.append(alertBar) - - let width = UIScreen.main.bounds.width - let height = UIScreen.main.bounds.height - - let baseView = UIView(frame: UIScreen.main.bounds) - baseView.isUserInteractionEnabled = false - baseView.addSubview(alertBar) - - let window: UIWindow - let orientation = UIApplication.shared.statusBarOrientation - if orientation.isLandscape { - window = UIWindow(frame: CGRect(x: 0, y: 0, width: height, height: width)) - let sign: CGFloat = orientation == .landscapeLeft ? -1 : 1 - let d = fabs(width - height) / 2 - baseView.transform = CGAffineTransform(rotationAngle: sign * CGFloat(M_PI) / 2).translatedBy(x: sign * d, y: sign * d) - } else { - window = UIWindow(frame: CGRect(x: 0, y: 0, width: width, height: height)) - if orientation == .portraitUpsideDown { - baseView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI)) - } - } - window.isUserInteractionEnabled = false - window.windowLevel = UIWindowLevelStatusBar + 1 + CGFloat(AlertBar.alertBars.count) - window.addSubview(baseView) - window.makeKeyAndVisible() - - alertBar.transform = CGAffineTransform(translationX: 0, y: -statusBarHeight) - UIView.animate(withDuration: 0.2, - animations: { () -> Void in - alertBar.transform = CGAffineTransform.identity - }, completion: { _ in - UIView.animate(withDuration: 0.2, - delay: duration, - options: UIViewAnimationOptions(), - animations: { () -> Void in - alertBar.transform = CGAffineTransform(translationX: 0, y: -statusBarHeight) - }, - completion: { (animated: Bool) -> Void in - alertBar.removeFromSuperview() - if let index = AlertBar.alertBars.index(of: alertBar) { - AlertBar.alertBars.remove(at: index) - } - // To hold window instance - window.isHidden = true - completion?() - }) - }) - } - - open class func show(error: Error, duration: Double = 2, completion: (() -> Void)? = nil) { - let code = (error as NSError).code - let localizedDescription = error.localizedDescription - self.show(.error, message: "(\(code)) " + localizedDescription, duration: duration, completion: completion) - } -} - -internal extension UIColor { - convenience init(_ rgbValue: UInt) { - self.init( - red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, - green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, - blue: CGFloat(rgbValue & 0x0000FF) / 255.0, - alpha: CGFloat(1.0) - ) - } -} diff --git a/Sources/AlertBar.swift b/Sources/AlertBar.swift index 8e4ffe9..0ebe226 100644 --- a/Sources/AlertBar.swift +++ b/Sources/AlertBar.swift @@ -95,11 +95,11 @@ open class AlertBar: UIView { window = UIWindow(frame: CGRect(x: 0, y: 0, width: height, height: width)) let sign: CGFloat = orientation == .landscapeLeft ? -1 : 1 let d = fabs(width - height) / 2 - baseView.transform = CGAffineTransform(rotationAngle: sign * CGFloat(M_PI) / 2).translatedBy(x: sign * d, y: sign * d) + baseView.transform = CGAffineTransform(rotationAngle: sign * CGFloat.pi / 2).translatedBy(x: sign * d, y: sign * d) } else { window = UIWindow(frame: CGRect(x: 0, y: 0, width: width, height: height)) if orientation == .portraitUpsideDown { - baseView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI)) + baseView.transform = CGAffineTransform(rotationAngle: CGFloat.pi) } } window.isUserInteractionEnabled = false