Skip to content

Commit

Permalink
Merge pull request #51 from nsuxin/Swift4
Browse files Browse the repository at this point in the history
Support for Swift4
  • Loading branch information
johnlui committed Oct 26, 2017
2 parents a335414 + 1ff2c87 commit 5d4d802
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
89 changes: 47 additions & 42 deletions SwiftNotice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
})
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 5d4d802

Please sign in to comment.