Skip to content

Commit

Permalink
add new api for closing each single notice
Browse files Browse the repository at this point in the history
  • Loading branch information
zzjzz9266a committed Apr 5, 2017
1 parent 8e0d503 commit e5e5d87
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 52 deletions.
97 changes: 64 additions & 33 deletions SwiftNotice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,54 @@ private let sn_topBar: Int = 1001

extension UIResponder {
/// wait with your own animated images
func pleaseWaitWithImages(_ imageNames: Array<UIImage>, timeInterval: Int) {
SwiftNotice.wait(imageNames, timeInterval: timeInterval)
@discardableResult
func pleaseWaitWithImages(_ imageNames: Array<UIImage>, timeInterval: Int) -> UIWindow{
return SwiftNotice.wait(imageNames, timeInterval: timeInterval)
}
// api changed from v3.3
func noticeTop(_ text: String, autoClear: Bool = true, autoClearTime: Int = 1) {
SwiftNotice.noticeOnStatusBar(text, autoClear: autoClear, autoClearTime: autoClearTime)
@discardableResult
func noticeTop(_ text: String, autoClear: Bool = true, autoClearTime: Int = 1) -> UIWindow{
return SwiftNotice.noticeOnStatusBar(text, autoClear: autoClear, autoClearTime: autoClearTime)
}

// new apis from v3.3
func noticeSuccess(_ text: String, autoClear: Bool = false, autoClearTime: Int = 3) {
SwiftNotice.showNoticeWithText(NoticeType.success, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
@discardableResult
func noticeSuccess(_ text: String, autoClear: Bool = false, autoClearTime: Int = 3) -> UIWindow{
return SwiftNotice.showNoticeWithText(NoticeType.success, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
}
func noticeError(_ text: String, autoClear: Bool = false, autoClearTime: Int = 3) {
SwiftNotice.showNoticeWithText(NoticeType.error, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
@discardableResult
func noticeError(_ text: String, autoClear: Bool = false, autoClearTime: Int = 3) -> UIWindow{
return SwiftNotice.showNoticeWithText(NoticeType.error, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
}
func noticeInfo(_ text: String, autoClear: Bool = false, autoClearTime: Int = 3) {
SwiftNotice.showNoticeWithText(NoticeType.info, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
@discardableResult
func noticeInfo(_ text: String, autoClear: Bool = false, autoClearTime: Int = 3) -> UIWindow{
return SwiftNotice.showNoticeWithText(NoticeType.info, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
}

// old apis
func successNotice(_ text: String, autoClear: Bool = true) {
SwiftNotice.showNoticeWithText(NoticeType.success, text: text, autoClear: autoClear, autoClearTime: 3)
@discardableResult
func successNotice(_ text: String, autoClear: Bool = true) -> UIWindow{
return SwiftNotice.showNoticeWithText(NoticeType.success, text: text, autoClear: autoClear, autoClearTime: 3)
}
func errorNotice(_ text: String, autoClear: Bool = true) {
SwiftNotice.showNoticeWithText(NoticeType.error, text: text, autoClear: autoClear, autoClearTime: 3)
@discardableResult
func errorNotice(_ text: String, autoClear: Bool = true) -> UIWindow{
return SwiftNotice.showNoticeWithText(NoticeType.error, text: text, autoClear: autoClear, autoClearTime: 3)
}
func infoNotice(_ text: String, autoClear: Bool = true) {
SwiftNotice.showNoticeWithText(NoticeType.info, text: text, autoClear: autoClear, autoClearTime: 3)
@discardableResult
func infoNotice(_ text: String, autoClear: Bool = true) -> UIWindow{
return SwiftNotice.showNoticeWithText(NoticeType.info, text: text, autoClear: autoClear, autoClearTime: 3)
}
func notice(_ text: String, type: NoticeType, autoClear: Bool, autoClearTime: Int = 3) {
SwiftNotice.showNoticeWithText(type, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
@discardableResult
func notice(_ text: String, type: NoticeType, autoClear: Bool, autoClearTime: Int = 3) -> UIWindow{
return SwiftNotice.showNoticeWithText(type, text: text, autoClear: autoClear, autoClearTime: autoClearTime)
}
func pleaseWait() {
SwiftNotice.wait()
@discardableResult
func pleaseWait() -> UIWindow{
return SwiftNotice.wait()
}
func noticeOnlyText(_ text: String) {
SwiftNotice.showText(text)
@discardableResult
func noticeOnlyText(_ text: String) -> UIWindow{
return SwiftNotice.showText(text)
}
func clearAllNotice() {
SwiftNotice.clear()
Expand Down Expand Up @@ -89,7 +100,8 @@ class SwiftNotice: NSObject {
windows.removeAll(keepingCapacity: false)
}

static func noticeOnStatusBar(_ text: String, autoClear: Bool, autoClearTime: Int) {
@discardableResult
static func noticeOnStatusBar(_ text: String, autoClear: Bool, autoClearTime: Int) -> UIWindow{
let frame = UIApplication.shared.statusBarFrame
let window = UIWindow()
window.backgroundColor = UIColor.clear
Expand Down Expand Up @@ -118,7 +130,7 @@ class SwiftNotice: NSObject {
window.center = CGPoint(x: x, y: y)

// change direction
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * M_PI / 180))
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * Double.pi / 180))
}

window.windowLevel = UIWindowLevelStatusBar
Expand All @@ -140,9 +152,11 @@ class SwiftNotice: NSObject {
self.perform(selector, with: window, afterDelay: TimeInterval(autoClearTime))
}
})

return window
}
static func wait(_ imageNames: Array<UIImage> = Array<UIImage>(), timeInterval: Int = 0) {

@discardableResult
static func wait(_ imageNames: Array<UIImage> = Array<UIImage>(), timeInterval: Int = 0) -> UIWindow {
let frame = CGRect(x: 0, y: 0, width: 78, height: 78)
let window = UIWindow()
window.backgroundColor = UIColor.clear
Expand Down Expand Up @@ -181,7 +195,7 @@ class SwiftNotice: NSObject {
// change center
window.center = getRealCenter()
// change direction
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * M_PI / 180))
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * Double.pi / 180))
}

window.windowLevel = UIWindowLevelAlert
Expand All @@ -193,8 +207,11 @@ class SwiftNotice: NSObject {
UIView.animate(withDuration: 0.2, animations: {
mainView.alpha = 1
})
return window
}
static func showText(_ text: String) {

@discardableResult
static func showText(_ text: String, autoClear: Bool=true, autoClearTime: Int=2) -> UIWindow {
let window = UIWindow()
window.backgroundColor = UIColor.clear
let mainView = UIView()
Expand Down Expand Up @@ -222,16 +239,23 @@ class SwiftNotice: NSObject {
// change center
window.center = getRealCenter()
// change direction
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * M_PI / 180))
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * Double.pi / 180))
}

window.windowLevel = UIWindowLevelAlert
window.isHidden = false
window.addSubview(mainView)
windows.append(window)

if autoClear {
let selector = #selector(SwiftNotice.hideNotice(_:))
self.perform(selector, with: window, afterDelay: TimeInterval(autoClearTime))
}
return window
}

static func showNoticeWithText(_ type: NoticeType,text: String, autoClear: Bool, autoClearTime: Int) {
@discardableResult
static func showNoticeWithText(_ type: NoticeType,text: String, autoClear: Bool, autoClearTime: Int) -> UIWindow {
let frame = CGRect(x: 0, y: 0, width: 90, height: 90)
let window = UIWindow()
window.backgroundColor = UIColor.clear
Expand Down Expand Up @@ -268,7 +292,7 @@ class SwiftNotice: NSObject {
// change center
window.center = getRealCenter()
// change direction
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * M_PI / 180))
window.transform = CGAffineTransform(rotationAngle: CGFloat(degree * Double.pi / 180))
}

window.windowLevel = UIWindowLevelAlert
Expand All @@ -286,6 +310,7 @@ class SwiftNotice: NSObject {
let selector = #selector(SwiftNotice.hideNotice(_:))
self.perform(selector, with: window, afterDelay: TimeInterval(autoClearTime))
}
return window
}

// fix https://github.com/johnlui/SwiftNotice/issues/2
Expand Down Expand Up @@ -333,7 +358,7 @@ class SwiftNoticeSDK {

// draw circle
checkmarkShapePath.move(to: CGPoint(x: 36, y: 18))
checkmarkShapePath.addArc(withCenter: CGPoint(x: 18, y: 18), radius: 17.5, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
checkmarkShapePath.addArc(withCenter: CGPoint(x: 18, y: 18), radius: 17.5, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: true)
checkmarkShapePath.close()

switch type {
Expand Down Expand Up @@ -361,7 +386,7 @@ class SwiftNoticeSDK {

let checkmarkShapePath = UIBezierPath()
checkmarkShapePath.move(to: CGPoint(x: 18, y: 27))
checkmarkShapePath.addArc(withCenter: CGPoint(x: 18, y: 27), radius: 1, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
checkmarkShapePath.addArc(withCenter: CGPoint(x: 18, y: 27), radius: 1, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: true)
checkmarkShapePath.close()

UIColor.white.setFill()
Expand Down Expand Up @@ -408,3 +433,9 @@ class SwiftNoticeSDK {
return Cache.imageOfInfo!
}
}

extension UIWindow{
func hide(){
SwiftNotice.hideNotice(self)
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0830"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "460ABCC91ADE87C000B43A4E"
BuildableName = "SwiftNoticeExample.app"
BlueprintName = "SwiftNoticeExample"
ReferencedContainer = "container:SwiftNoticeExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "460ABCDE1ADE87C000B43A4E"
BuildableName = "SwiftNoticeExampleTests.xctest"
BlueprintName = "SwiftNoticeExampleTests"
ReferencedContainer = "container:SwiftNoticeExample.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "460ABCC91ADE87C000B43A4E"
BuildableName = "SwiftNoticeExample.app"
BlueprintName = "SwiftNoticeExample"
ReferencedContainer = "container:SwiftNoticeExample.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "460ABCC91ADE87C000B43A4E"
BuildableName = "SwiftNoticeExample.app"
BlueprintName = "SwiftNoticeExample"
ReferencedContainer = "container:SwiftNoticeExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "460ABCC91ADE87C000B43A4E"
BuildableName = "SwiftNoticeExample.app"
BlueprintName = "SwiftNoticeExample"
ReferencedContainer = "container:SwiftNoticeExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SwiftNoticeExample.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>460ABCC91ADE87C000B43A4E</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>460ABCDE1ADE87C000B43A4E</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
Loading

0 comments on commit e5e5d87

Please sign in to comment.