Skip to content
This repository has been archived by the owner on Oct 6, 2019. It is now read-only.

Commit

Permalink
Add ability to reset display timer
Browse files Browse the repository at this point in the history
  • Loading branch information
msaps committed Jul 26, 2016
1 parent 7b09449 commit d6f9e83
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions Sources/CocoaBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public class CocoaBar: UIView, CocoaBarLayoutDelegate {
private var isAnimating: Bool = false

private var displayTimer: NSTimer?
private var currentDisplayDuration: Double = -1.0

// MARK: Properties

Expand Down Expand Up @@ -331,18 +332,26 @@ public class CocoaBar: UIView, CocoaBarLayoutDelegate {
}
}

private func setUpDisplayTimer(duration: Double) {
if self.displayTimer == nil {
self.displayTimer = NSTimer.scheduledTimerWithTimeInterval(duration,
target: self,
selector: #selector(displayTimerElapsed),
userInfo: nil,
repeats: false)
private func setUpDisplayTimer(duration: Double, destroyCurrentTimer: Bool) {
if !self.isAnimating {

if destroyCurrentTimer {
self.destroyDisplayTimer()
}
if self.displayTimer == nil {
self.currentDisplayDuration = duration
self.displayTimer = NSTimer.scheduledTimerWithTimeInterval(duration,
target: self,
selector: #selector(displayTimerElapsed),
userInfo: nil,
repeats: false)
}
}
}

private func destroyDisplayTimer() {
if let displayTimer = self.displayTimer {
self.currentDisplayDuration = -1.0
displayTimer.invalidate()
self.displayTimer = nil
}
Expand Down Expand Up @@ -417,7 +426,7 @@ public class CocoaBar: UIView, CocoaBarLayoutDelegate {
{ (completed) in
self.isShowing = true
self.isAnimating = false
self.setUpDisplayTimer(duration)
self.setUpDisplayTimer(duration, destroyCurrentTimer: true)

if let delegate = self.delegate {
delegate.cocoaBar(self, didShowAnimated: animated)
Expand All @@ -439,7 +448,7 @@ public class CocoaBar: UIView, CocoaBarLayoutDelegate {
self.layout.showShadowAnimated(animated)
self.layoutIfNeeded()
self.isShowing = true
self.setUpDisplayTimer(duration)
self.setUpDisplayTimer(duration, destroyCurrentTimer: true)

if let completion = completion {
completion(animated: animated, completed: true, visible: self.isShowing)
Expand Down Expand Up @@ -618,6 +627,35 @@ public class CocoaBar: UIView, CocoaBarLayoutDelegate {
completion: completion)
}

/**
Reset the current display timer if it exists with the current display duration.
*/
public func resetDisplayTimer() {
if self.currentDisplayDuration > 0.0 {
self.resetDisplayTimer(self.currentDisplayDuration)
}
}

/**
Reset the current display timer if it exists.

:param: duration The display duration to reset the timer with.
*/
public func resetDisplayTimer(duration: DisplayDuration) {
self.resetDisplayTimer(duration.value)
}

/**
Reset the current display timer if it exists.

:param: duration The display duration to reset the timer with.
*/
public func resetDisplayTimer(duration: Double) {
if self.displayTimer != nil {
self.setUpDisplayTimer(duration, destroyCurrentTimer: true)
}
}

// MARK: KeyCocoaBar

/**
Expand Down

0 comments on commit d6f9e83

Please sign in to comment.