diff --git a/SwiftNotice.swift b/SwiftNotice.swift index a3898de..80854a9 100644 --- a/SwiftNotice.swift +++ b/SwiftNotice.swift @@ -10,8 +10,12 @@ import Foundation import UIKit extension UIViewController { + /// wait with your own animated images + func pleaseWaitWithImages(imageNames: Array, timeInterval: Int) { + SwiftNotice.wait(imageNames, timeInterval: timeInterval) + } // api changed from v3.3 - func noticeTop(text: String, autoClear: Bool = false, autoClearTime: Int = 1) { + func noticeTop(text: String, autoClear: Bool = true, autoClearTime: Int = 1) { SwiftNotice.noticeOnSatusBar(text, autoClear: autoClear, autoClearTime: autoClearTime) } @@ -60,11 +64,34 @@ class SwiftNotice: NSObject { static var windows = Array() static let rv = UIApplication.sharedApplication().keyWindow?.subviews.first as UIView! + static var timer: dispatch_source_t! + static var timerTimes = 0 + static var degree: Double { + get { + return [0, 0, 180, 270, 90][UIApplication.sharedApplication().statusBarOrientation.hashValue] as Double + } + } + static var center: CGPoint { + get { + var array = [UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height] + array = array.sort(<) + let screenWidth = array[0] + let screenHeight = array[1] + let x = [0, screenWidth/2, screenWidth/2, 10, screenWidth-10][UIApplication.sharedApplication().statusBarOrientation.hashValue] as CGFloat + let y = [0, 10, screenHeight-10, screenHeight/2, screenHeight/2][UIApplication.sharedApplication().statusBarOrientation.hashValue] as CGFloat + return CGPointMake(x, y) + } + } // fix https://github.com/johnlui/SwiftNotice/issues/2 // thanks broccolii(https://github.com/broccolii) and his PR https://github.com/johnlui/SwiftNotice/pull/5 static func clear() { self.cancelPreviousPerformRequestsWithTarget(self) + if let _ = timer { + dispatch_source_cancel(timer) + timer = nil + timerTimes = 0 + } windows.removeAll(keepCapacity: false) } @@ -87,6 +114,9 @@ class SwiftNotice: NSObject { window.windowLevel = UIWindowLevelStatusBar window.hidden = false + // change orientation + window.center = center + window.transform = CGAffineTransformMakeRotation(CGFloat(degree * M_PI / 180)) window.addSubview(view) windows.append(window) @@ -95,7 +125,7 @@ class SwiftNotice: NSObject { self.performSelector(selector, withObject: window, afterDelay: NSTimeInterval(autoClearTime)) } } - static func wait() { + static func wait(imageNames: Array = Array(), timeInterval: Int = 0) { let frame = CGRectMake(0, 0, 78, 78) let window = UIWindow() window.backgroundColor = UIColor.clearColor() @@ -103,16 +133,35 @@ class SwiftNotice: NSObject { mainView.layer.cornerRadius = 12 mainView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8) - let ai = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge) - ai.frame = CGRectMake(21, 21, 36, 36) - ai.startAnimating() - mainView.addSubview(ai) - + if imageNames.count > 0 { + if imageNames.count > timerTimes { + let iv = UIImageView(frame: frame) + iv.image = imageNames.first! + iv.contentMode = UIViewContentMode.ScaleAspectFit + mainView.addSubview(iv) + timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()) + dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, UInt64(timeInterval) * NSEC_PER_MSEC, 0) + dispatch_source_set_event_handler(timer, { () -> Void in + let name = imageNames[timerTimes % imageNames.count] + iv.image = name + timerTimes++ + }) + dispatch_resume(timer) + } + } else { + let ai = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge) + ai.frame = CGRectMake(21, 21, 36, 36) + ai.startAnimating() + mainView.addSubview(ai) + } + window.frame = frame mainView.frame = frame window.windowLevel = UIWindowLevelAlert - window.center = rv.center + window.center = getRealCenter() + // change orientation + window.transform = CGAffineTransformMakeRotation(CGFloat(degree * M_PI / 180)) window.hidden = false window.addSubview(mainView) windows.append(window) @@ -140,7 +189,9 @@ class SwiftNotice: NSObject { label.center = mainView.center window.windowLevel = UIWindowLevelAlert - window.center = rv.center + window.center = getRealCenter() + // change orientation + window.transform = CGAffineTransformMakeRotation(CGFloat(degree * M_PI / 180)) window.hidden = false window.addSubview(mainView) windows.append(window) @@ -178,11 +229,13 @@ class SwiftNotice: NSObject { mainView.frame = frame window.windowLevel = UIWindowLevelAlert - window.center = rv.center + window.center = getRealCenter() + // change orientation + window.transform = CGAffineTransformMakeRotation(CGFloat(degree * M_PI / 180)) window.hidden = false window.addSubview(mainView) windows.append(window) - + if autoClear { let selector = Selector("hideNotice:") self.performSelector(selector, withObject: window, afterDelay: NSTimeInterval(autoClearTime)) @@ -199,6 +252,15 @@ class SwiftNotice: NSObject { } } } + + // fix orientation problem + static func getRealCenter() -> CGPoint { + if UIApplication.sharedApplication().statusBarOrientation.hashValue >= 3 { + return CGPoint(x: rv.center.y, y: rv.center.x) + } else { + return rv.center + } + } } class SwiftNoticeSDK { diff --git a/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/leqicheng.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/leqicheng.xcuserdatad/UserInterfaceState.xcuserstate index 24e0f9d..5719739 100644 Binary files a/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/leqicheng.xcuserdatad/UserInterfaceState.xcuserstate and b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.xcworkspace/xcuserdata/leqicheng.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/Contents.json new file mode 100644 index 0000000..da4a164 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading1.imageset/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading1.imageset/Contents.json new file mode 100644 index 0000000..4542f06 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading1.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "loading1@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading1.imageset/loading1@2x.png b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading1.imageset/loading1@2x.png new file mode 100644 index 0000000..1b4709e Binary files /dev/null and b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading1.imageset/loading1@2x.png differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading2.imageset/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading2.imageset/Contents.json new file mode 100644 index 0000000..e312f06 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading2.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "loading2@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading2.imageset/loading2@2x.png b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading2.imageset/loading2@2x.png new file mode 100644 index 0000000..04b4448 Binary files /dev/null and b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading2.imageset/loading2@2x.png differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading3.imageset/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading3.imageset/Contents.json new file mode 100644 index 0000000..990fea2 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading3.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "loading3@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading3.imageset/loading3@2x.png b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading3.imageset/loading3@2x.png new file mode 100644 index 0000000..2723484 Binary files /dev/null and b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading3.imageset/loading3@2x.png differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading4.imageset/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading4.imageset/Contents.json new file mode 100644 index 0000000..de1a2e6 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading4.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "loading4@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading4.imageset/loading4@2x.png b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading4.imageset/loading4@2x.png new file mode 100644 index 0000000..8dbc622 Binary files /dev/null and b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading4.imageset/loading4@2x.png differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading5.imageset/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading5.imageset/Contents.json new file mode 100644 index 0000000..13ae6b8 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading5.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "loading5@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading5.imageset/loading5@2x.png b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading5.imageset/loading5@2x.png new file mode 100644 index 0000000..e4cee0b Binary files /dev/null and b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading5.imageset/loading5@2x.png differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading6.imageset/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading6.imageset/Contents.json new file mode 100644 index 0000000..384d6be --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading6.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "loading6@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading6.imageset/loading6@2x.png b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading6.imageset/loading6@2x.png new file mode 100644 index 0000000..a65556d Binary files /dev/null and b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading6.imageset/loading6@2x.png differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading7.imageset/Contents.json b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading7.imageset/Contents.json new file mode 100644 index 0000000..a32c270 --- /dev/null +++ b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading7.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "loading7@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading7.imageset/loading7@2x.png b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading7.imageset/loading7@2x.png new file mode 100644 index 0000000..72ae3c5 Binary files /dev/null and b/SwiftNoticeExample/SwiftNoticeExample/Images.xcassets/loading7.imageset/loading7@2x.png differ diff --git a/SwiftNoticeExample/SwiftNoticeExample/Info.plist b/SwiftNoticeExample/SwiftNoticeExample/Info.plist index 4576092..05ca1a5 100644 --- a/SwiftNoticeExample/SwiftNoticeExample/Info.plist +++ b/SwiftNoticeExample/SwiftNoticeExample/Info.plist @@ -39,6 +39,7 @@ UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + UIInterfaceOrientationPortraitUpsideDown diff --git a/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift b/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift index c92c044..28e458c 100644 --- a/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift +++ b/SwiftNoticeExample/SwiftNoticeExample/ViewController.swift @@ -24,7 +24,12 @@ class ViewController: UIViewController { self.noticeTop("OK!") } @IBAction func wait(sender: AnyObject) { - self.pleaseWait() +// self.pleaseWait() + var imagesArray = Array() + for i in 1...7 { + imagesArray.append(UIImage(named: "loading\(i)")!) + } + self.pleaseWaitWithImages(imagesArray, timeInterval: 50) } @IBAction func noticeSuccess(sender: AnyObject) { self.successNotice("Success!")