From 1ff2c870c06e78ced25e424c2e367584e0549cbf Mon Sep 17 00:00:00 2001 From: Xin <10236240@qq.com> Date: Thu, 26 Oct 2017 12:15:51 +0800 Subject: [PATCH] Support for Swift4 --- SwiftNotice.swift | 89 ++++++++++--------- .../project.pbxproj | 6 +- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/SwiftNotice.swift b/SwiftNotice.swift index 96cc599..3285982 100644 --- a/SwiftNotice.swift +++ b/SwiftNotice.swift @@ -137,22 +137,21 @@ class SwiftNotice: NSObject { window.isHidden = false window.addSubview(view) windows.append(window) - + var origPoint = view.frame.origin origPoint.y = -(view.frame.size.height) let destPoint = view.frame.origin view.tag = sn_topBar - + view.frame = CGRect(origin: origPoint, size: view.frame.size) UIView.animate(withDuration: 0.3, animations: { - view.frame = CGRect(origin: destPoint, size: view.frame.size) + view.frame = CGRect(origin: destPoint, size: view.frame.size) }, completion: { b in - if autoClear { - let selector = #selector(SwiftNotice.hideNotice(_:)) - self.perform(selector, with: window, afterDelay: TimeInterval(autoClearTime)) - } + if autoClear { + self.perform(.hideNotice, with: window, afterDelay: TimeInterval(autoClearTime)) + } }) - return window + return window } @discardableResult @@ -171,7 +170,7 @@ class SwiftNotice: NSObject { iv.contentMode = UIViewContentMode.scaleAspectFit mainView.addSubview(iv) timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)), queue: DispatchQueue.main) as! DispatchSource - timer.scheduleRepeating(deadline: DispatchTime.now(), interval: DispatchTimeInterval.milliseconds(timeInterval)) + timer.schedule(deadline: DispatchTime.now(), repeating: DispatchTimeInterval.milliseconds(timeInterval)) timer.setEventHandler(handler: { () -> Void in let name = imageNames[timerTimes % imageNames.count] iv.image = name @@ -202,10 +201,10 @@ class SwiftNotice: NSObject { window.isHidden = false window.addSubview(mainView) windows.append(window) - + mainView.alpha = 0.0 UIView.animate(withDuration: 0.2, animations: { - mainView.alpha = 1 + mainView.alpha = 1 }) return window } @@ -249,8 +248,7 @@ class SwiftNotice: NSObject { windows.append(window) if autoClear { - let selector = #selector(SwiftNotice.hideNotice(_:)) - self.perform(selector, with: window, afterDelay: TimeInterval(autoClearTime)) + self.perform(.hideNotice, with: window, afterDelay: TimeInterval(autoClearTime)) } return window } @@ -301,42 +299,17 @@ class SwiftNotice: NSObject { window.isHidden = false window.addSubview(mainView) windows.append(window) - + mainView.alpha = 0.0 UIView.animate(withDuration: 0.2, animations: { - mainView.alpha = 1 + mainView.alpha = 1 }) - + if autoClear { - let selector = #selector(SwiftNotice.hideNotice(_:)) - self.perform(selector, with: window, afterDelay: TimeInterval(autoClearTime)) + self.perform(.hideNotice, with: window, afterDelay: TimeInterval(autoClearTime)) } return window } - - // fix https://github.com/johnlui/SwiftNotice/issues/2 - static func hideNotice(_ sender: AnyObject) { - if let window = sender as? UIWindow { - - if let v = window.subviews.first { - UIView.animate(withDuration: 0.2, animations: { - - if v.tag == sn_topBar { - v.frame = CGRect(x: 0, y: -v.frame.height, width: v.frame.width, height: v.frame.height) - } - v.alpha = 0 - }, completion: { b in - - if let index = windows.index(where: { (item) -> Bool in - return item == window - }) { - windows.remove(at: index) - } - }) - } - - } - } // just for iOS 8 static func getRealCenter() -> CGPoint { @@ -440,3 +413,35 @@ extension UIWindow{ SwiftNotice.hideNotice(self) } } + +fileprivate extension Selector { + static let hideNotice = #selector(SwiftNotice.hideNotice(_:)) +} + +@objc extension SwiftNotice { + + // fix https://github.com/johnlui/SwiftNotice/issues/2 + static func hideNotice(_ sender: AnyObject) { + if let window = sender as? UIWindow { + + if let v = window.subviews.first { + UIView.animate(withDuration: 0.2, animations: { + + if v.tag == sn_topBar { + v.frame = CGRect(x: 0, y: -v.frame.height, width: v.frame.width, height: v.frame.height) + } + v.alpha = 0 + }, completion: { b in + + if let index = windows.index(where: { (item) -> Bool in + return item == window + }) { + windows.remove(at: index) + } + }) + } + + } + } + +} diff --git a/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.pbxproj b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.pbxproj index 65cd187..cff2ee8 100644 --- a/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.pbxproj +++ b/SwiftNoticeExample/SwiftNoticeExample.xcodeproj/project.pbxproj @@ -305,6 +305,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -341,6 +342,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_VERSION = 4.0; VALIDATE_PRODUCT = YES; }; name = Release; @@ -354,7 +356,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.lvwenhan.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -368,7 +370,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.lvwenhan.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release;