Skip to content

Commit

Permalink
1. Removed delegate metghods, continued with UIControl .valueChanged …
Browse files Browse the repository at this point in the history
…options and tags
  • Loading branch information
Jaleel Nazir committed Aug 6, 2018
2 parents d659f3c + 721aa63 commit 0b56693
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
28 changes: 2 additions & 26 deletions MJMaterialSwitch/MJMaterialSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -528,7 +512,6 @@ class MJMaterialSwitch: UIControl {
self.isOn = false
self.sendActions(for: UIControlEvents.valueChanged)
}
self.isOn = false
}

// Initialize and appear ripple effect
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -698,5 +675,4 @@ class MJMaterialSwitch: UIControl {
}
}
}

}
33 changes: 25 additions & 8 deletions MJMaterialSwitch/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@

import UIKit

class ViewController: UIViewController, MJMaterialSwitchDelegate {
class ViewController: UIViewController {

// Tags
let kSmallSwitch = 1
let kMediumSwitch = 2
let kLargeSwitch = 3

var androidSwitchSmall : MJMaterialSwitch!

override func viewDidLoad() {
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
Expand All @@ -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")

}
}
}

0 comments on commit 0b56693

Please sign in to comment.