Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created September 7, 2016 09:21
Show Gist options
  • Save greggjaskiewicz/99efb8cd0f1c5dbdc29113b09b6b2114 to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/99efb8cd0f1c5dbdc29113b09b6b2114 to your computer and use it in GitHub Desktop.
final class PulsateViewAnimator: NSObject {
   private weak var view: UIView?
   private var timer: Timer?
   private let breathOutDuration: TimeInterval
   private let holdDuration: TimeInterval
   private let breathInDuration: TimeInterval
   private let pulseScaleIdentity: CGAffineTransform
   init(view: UIView, pulseScale: CGFloat,
        breathOutDuration: TimeInterval,
        holdDuration: TimeInterval,
        breathInDuration: TimeInterval,
        intervalBetweenPulses: TimeInterval)
   {
       self.view = view
       self.breathInDuration = breathInDuration
       self.breathOutDuration = breathOutDuration
       self.holdDuration = holdDuration
       self.pulseScaleIdentity = CGAffineTransform.init(scaleX: pulseScale, y: pulseScale)
       super.init()
       self.timer = Timer.scheduledTimer(timeInterval: intervalBetweenPulses, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
   }
   @objc func timerAction(timer: Timer) {
       UIView.animate(withDuration: self.breathOutDuration, delay: self.holdDuration, options: .curveEaseInOut, animations: {
           self.view?.transform = self.pulseScaleIdentity
       }) { (completed) in
           if (completed) {
               UIView.animate(withDuration: self.breathInDuration, delay: self.holdDuration, options: .curveEaseInOut,
                              animations: {
                               self.view?.transform = CGAffineTransform.identity
                   }, completion: { (_) in })
           }
       }
   }
   deinit {
       self.timer?.invalidate()
       self.view = nil
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment