From 81cccf323ba69ae938ac4c6ff8f1ae97bc2f81f3 Mon Sep 17 00:00:00 2001 From: Yarik Arsenkin Date: Wed, 30 Sep 2015 20:23:33 +0200 Subject: [PATCH] Adds delegate protocol and notification Adds delegate protocol and notification when switch animation is finished. --- SevenSwitch.swift | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/SevenSwitch.swift b/SevenSwitch.swift index c732318..e861143 100644 --- a/SevenSwitch.swift +++ b/SevenSwitch.swift @@ -25,10 +25,23 @@ import UIKit import QuartzCore +@objc protocol SevenSwitchDelegate { + + /* + * Notifies the delegate, that switch animation has completed. + */ + func didFinishAnimating() +} + @IBDesignable @objc public class SevenSwitch: UIControl { // public + /* + * Delegate object, that is going to be notified when switch animation is completed. + */ + var delegate: AnyObject? + /* * Set (without animation) whether the switch is on or off */ @@ -442,6 +455,7 @@ import QuartzCore self.offLabel.alpha = 0 }, completion: { finished in self.isAnimating = false + self.notifyDelegate() }) } else { @@ -493,6 +507,7 @@ import QuartzCore }, completion: { finished in self.isAnimating = false + self.notifyDelegate() }) } else { @@ -515,5 +530,15 @@ import QuartzCore currentVisualValue = false } + func notifyDelegate() { + let selector: Selector = "didFinishAnimating" + if delegate != nil { + if delegate!.respondsToSelector(selector) { + dispatch_async(dispatch_get_main_queue(), { () -> Void in + delegate!.performSelector(selector) + }) + } + } + } }