diff --git a/MJMaterialSwitch/MJMaterialSwitch.swift b/MJMaterialSwitch/MJMaterialSwitch.swift index b728dfb..8b7d8b5 100644 --- a/MJMaterialSwitch/MJMaterialSwitch.swift +++ b/MJMaterialSwitch/MJMaterialSwitch.swift @@ -23,17 +23,10 @@ public enum MJMaterialSwitchSize { case big, normal, small } - -protocol MJMaterialSwitchDelegate: class { - func switchStateChanged(_ currentState: MJMaterialSwitchState) -} - class MJMaterialSwitch: UIControl { //MARK: - Properties - //MARK: Delegate - weak var delegate: MJMaterialSwitchDelegate? = nil //MARK: State /** A Boolean value that represents switch's current state(ON/OFF). YES to ON, NO to OFF the switch */ @@ -385,12 +378,6 @@ class MJMaterialSwitch: UIControl { //The event handling method @objc func switchAreaTapped(recognizer: UITapGestureRecognizer) { - // Delegate method - if self.isOn { - self.delegate?.switchStateChanged(.off) - } else { - self.delegate?.switchStateChanged(.on) - } self.changeThumbState() } @@ -427,11 +414,10 @@ class MJMaterialSwitch: UIControl { }) { (finished) in // change state to ON - if self.isOn { + if !self.isOn { self.isOn = true // Expressly put this code here to change surely and send action correctly self.sendActions(for: UIControlEvents.valueChanged) } - self.isOn = true // print("now isOn: %d", self.isOn) // print("thumb end pos: %@", NSStringFromCGRect(self.switchThumb.frame)) // Bouncing effect: Move thumb a bit, for better UX @@ -471,7 +457,6 @@ class MJMaterialSwitch: UIControl { self.isOn = false // Expressly put this code here to change surely and send action correctly self.sendActions(for: UIControlEvents.valueChanged) } - self.isOn = false // print("now isOn: %d", self.isOn) // print("thumb end pos: %@", NSStringFromCGRect(self.switchThumb.frame)) // Bouncing effect: Move thumb a bit, for better UX @@ -502,11 +487,10 @@ class MJMaterialSwitch: UIControl { self.track.backgroundColor = self.trackDisabledTintColor } - if self.isOn { + if !self.isOn { self.isOn = true self.sendActions(for: UIControlEvents.valueChanged) } - self.isOn = true } // Without animation @@ -528,7 +512,6 @@ class MJMaterialSwitch: UIControl { self.isOn = false self.sendActions(for: UIControlEvents.valueChanged) } - self.isOn = false } // Initialize and appear ripple effect @@ -636,12 +619,6 @@ class MJMaterialSwitch: UIControl { // print("track midPosX: %f", CGRectGetMidX(self.track.frame)) // print("%@", NSStringFromCGRect(self.switchThumb.frame)) - // Delegate method - if self.isOn { - self.delegate?.switchStateChanged(.off) - } else { - self.delegate?.switchStateChanged(.on) - } self.changeThumbState() } @@ -698,5 +675,4 @@ class MJMaterialSwitch: UIControl { } } } - } diff --git a/MJMaterialSwitch/ViewController.swift b/MJMaterialSwitch/ViewController.swift index 848c206..b9536bf 100644 --- a/MJMaterialSwitch/ViewController.swift +++ b/MJMaterialSwitch/ViewController.swift @@ -8,7 +8,12 @@ import UIKit -class ViewController: UIViewController, MJMaterialSwitchDelegate { +class ViewController: UIViewController { + + // Tags + let kSmallSwitch = 1 + let kMediumSwitch = 2 + let kLargeSwitch = 3 var androidSwitchSmall : MJMaterialSwitch! @@ -16,18 +21,20 @@ class ViewController: UIViewController, MJMaterialSwitchDelegate { super.viewDidLoad() self.androidSwitchSmall = MJMaterialSwitch(withSize: .small, style: MJMaterialSwitchStyle.light, state: MJMaterialSwitchState.on) - self.androidSwitchSmall.delegate = self + self.androidSwitchSmall.tag = kSmallSwitch + self.androidSwitchSmall.addTarget(self, action: #selector(switchStateChanged(_:)), for: UIControlEvents.valueChanged) self.view.addSubview(self.androidSwitchSmall) let androidSwitchMedium = MJMaterialSwitch(withSize: .normal, style: MJMaterialSwitchStyle.medium, state: MJMaterialSwitchState.on) - androidSwitchMedium.delegate = self androidSwitchMedium.isBounceEnabled = false androidSwitchMedium.isRippleEnabled = true + androidSwitchMedium.tag = kMediumSwitch + androidSwitchMedium.addTarget(self, action: #selector(switchStateChanged(_:)), for: UIControlEvents.valueChanged) self.view.addSubview(androidSwitchMedium) - let androidSwitchLarge = MJMaterialSwitch(withSize: .big, style: MJMaterialSwitchStyle.dark, state: MJMaterialSwitchState.on) - androidSwitchLarge.delegate = self + androidSwitchLarge.tag = kLargeSwitch + androidSwitchLarge.addTarget(self, action: #selector(switchStateChanged(_:)), for: UIControlEvents.valueChanged) self.view.addSubview(androidSwitchLarge) self.androidSwitchSmall.center = self.view.center @@ -38,15 +45,25 @@ class ViewController: UIViewController, MJMaterialSwitchDelegate { androidSwitchLarge.center = self.view.center androidSwitchLarge.center.y = self.view.center.y + 50 - } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } - func switchStateChanged(_ currentState: MJMaterialSwitchState) { - print(currentState) + @objc func switchStateChanged(_ mjSwitch: MJMaterialSwitch) { + + if mjSwitch.tag == kSmallSwitch { + print(mjSwitch.isOn, mjSwitch.tag, "Small") + + } else if mjSwitch.tag == kMediumSwitch { + + print(mjSwitch.isOn, mjSwitch.tag, "Medium") + + } else if mjSwitch.tag == kLargeSwitch { + print(mjSwitch.isOn, mjSwitch.tag, "Large") + + } } }