diff --git a/README.md b/README.md index 321afdf..ef892c7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ SwiftNotice is a GUI library for displaying various popups written in pure Swift ##Features -![SwiftNotice gif](http://staticonsae.sinaapp.com/images/SwiftNotice2.gif) +![SwiftNotice gif](http://staticonsae.sinaapp.com/images/SwiftNotice3.gif) ##Pretty easy to use @@ -13,6 +13,8 @@ In any subclass of UIViewController: ```swift self.pleaseWait() +self.noticeTop("转发成功!") + self.successNotice("Success!") self.errorNotice("Error!") self.infoNotice("Info") @@ -31,8 +33,7 @@ Just clone and add `SwiftNotice.swift` to your project. ##Requirements * iOS 7.0+ -* Xcode 6.3 -* Swift 1.2 +* Xcode 6.3 (Swift 1.2) ##Contribution diff --git a/SwiftNotice.swift b/SwiftNotice.swift index cb1faca..92c625c 100644 --- a/SwiftNotice.swift +++ b/SwiftNotice.swift @@ -10,6 +10,12 @@ import Foundation import UIKit extension UIViewController { + func noticeTop(text: String) { + SwiftNotice.noticeOnSatusBar(text, autoClear: true) + } + func noticeTop(text: String, autoClear: Bool) { + SwiftNotice.noticeOnSatusBar(text, autoClear: autoClear) + } func successNotice(text: String) { SwiftNotice.showNoticeWithText(NoticeType.success, text: text, autoClear: true) } @@ -50,17 +56,40 @@ enum NoticeType{ class SwiftNotice: NSObject { - static var mainViews = Array() + static var windows = Array() static let rv = UIApplication.sharedApplication().keyWindow?.subviews.first as! UIView static func clear() { - for i in mainViews { - i.removeFromSuperview() - } + windows.removeAll(keepCapacity: false) } + static func noticeOnSatusBar(text: String, autoClear: Bool) { + let frame = UIApplication.sharedApplication().statusBarFrame + let window = UIWindow(frame: frame) + let view = UIView(frame: frame) + view.backgroundColor = UIColor(red: 0x6a/0x100, green: 0xb4/0x100, blue: 0x9f/0x100, alpha: 1) + + let label = UILabel(frame: frame) + label.textAlignment = NSTextAlignment.Center + label.font = UIFont.systemFontOfSize(12) + label.textColor = UIColor.whiteColor() + label.text = text + view.addSubview(label) + + window.windowLevel = UIWindowLevelStatusBar + window.hidden = false + window.addSubview(view) + windows.append(window) + + if autoClear { + let selector = Selector("hideNotice:") + self.performSelector(selector, withObject: view, afterDelay: 1) + } + } static func wait() { - let mainView = UIView(frame: CGRectMake(0, 0, 78, 78)) + let frame = CGRectMake(0, 0, 78, 78) + let window = UIWindow(frame: frame) + let mainView = UIView(frame: frame) mainView.layer.cornerRadius = 12 mainView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8) @@ -69,14 +98,15 @@ class SwiftNotice: NSObject { ai.startAnimating() mainView.addSubview(ai) - mainView.center = rv.center - rv.addSubview(mainView) - - mainViews.append(mainView) + window.windowLevel = UIWindowLevelAlert + window.center = rv.center + window.hidden = false + window.addSubview(mainView) + windows.append(window) } - static func showText(text: String) { let frame = CGRectMake(0, 0, 200, 60) + let window = UIWindow(frame: frame) let mainView = UIView(frame: frame) mainView.layer.cornerRadius = 12 mainView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8) @@ -88,14 +118,17 @@ class SwiftNotice: NSObject { label.textColor = UIColor.whiteColor() mainView.addSubview(label) - mainView.center = rv.center - rv.addSubview(mainView) - - mainViews.append(mainView) + window.windowLevel = UIWindowLevelAlert + window.center = rv.center + window.hidden = false + window.addSubview(mainView) + windows.append(window) } static func showNoticeWithText(type: NoticeType,text: String, autoClear: Bool) { - var mainView = UIView(frame: CGRectMake(0, 0, 90, 90)) + let frame = CGRectMake(0, 0, 90, 90) + let window = UIWindow(frame: frame) + let mainView = UIView(frame: frame) mainView.layer.cornerRadius = 10 mainView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.7) @@ -120,12 +153,13 @@ class SwiftNotice: NSObject { label.text = text label.textAlignment = NSTextAlignment.Center mainView.addSubview(label) - - mainView.center = rv.center - rv.addSubview(mainView) - - mainViews.append(mainView) - + + window.windowLevel = UIWindowLevelAlert + window.center = rv.center + window.hidden = false + window.addSubview(mainView) + windows.append(window) + if autoClear { let selector = Selector("hideNotice:") self.performSelector(selector, withObject: mainView, afterDelay: 3) diff --git a/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate index 65dc8e1..c656ca4 100644 Binary files a/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate and b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/xcuserdata/dren_a.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/xcuserdata/dren_a.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/xcuserdata/dren_a.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/SwiftNoticeExample/SwiftNoticeExample/Base.lproj/Main.storyboard b/SwiftNoticeExample/SwiftNoticeExample/Base.lproj/Main.storyboard index 1ff71fb..b9069ce 100644 --- a/SwiftNoticeExample/SwiftNoticeExample/Base.lproj/Main.storyboard +++ b/SwiftNoticeExample/SwiftNoticeExample/Base.lproj/Main.storyboard @@ -1,7 +1,7 @@ - + - + @@ -16,10 +16,10 @@ - + @@ -87,6 +97,8 @@ + + diff --git a/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift b/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift index 57ad1c1..f598d2b 100644 --- a/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift +++ b/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift @@ -20,6 +20,9 @@ class ViewController: UIViewController { // Dispose of any resources that can be recreated. } + @IBAction func topNotice(sender: AnyObject) { + self.noticeTop("转发成功!") + } @IBAction func wait(sender: AnyObject) { self.pleaseWait() }