Skip to content

Commit

Permalink
Update structure
Browse files Browse the repository at this point in the history
  • Loading branch information
knn90 committed Mar 20, 2017
1 parent 26c1c55 commit 1c80610
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 7 deletions.
4 changes: 1 addition & 3 deletions KNSwitcher.swift → KNSwitcher/KNSwitcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

protocol KNSwitcherChangeValueDelegate {
func switcherDidChangeValue(switcher:KNSwitcher,value:Bool)
func switcherDidChangeValue(switcher: KNSwitcher,value: Bool)
}

class KNSwitcher: UIView {
Expand All @@ -19,10 +19,8 @@ class KNSwitcher: UIView {
var delegate: KNSwitcherChangeValueDelegate?

@IBInspectable var on: Bool = false

@IBInspectable var originalImage:UIImage?
@IBInspectable var selectedImage:UIImage?

@IBInspectable var selectedColor:UIColor = UIColor(red: 126/255.0, green: 134/255.0, blue: 249/255.0, alpha: 1)
@IBInspectable var originalColor:UIColor = UIColor(red: 243/255.0, green: 229/255.0, blue: 211/255.0, alpha: 1)

Expand Down
16 changes: 12 additions & 4 deletions SwitcherExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
objects = {

/* Begin PBXBuildFile section */
7E1424761E7FB0630078B9D7 /* KNSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E1424751E7FB0630078B9D7 /* KNSwitcher.swift */; };
D6BDB3991BE768030009E292 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BDB3981BE768030009E292 /* AppDelegate.swift */; };
D6BDB39B1BE768030009E292 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BDB39A1BE768030009E292 /* ViewController.swift */; };
D6BDB39E1BE768030009E292 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D6BDB39C1BE768030009E292 /* Main.storyboard */; };
D6BDB3A01BE768030009E292 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D6BDB39F1BE768030009E292 /* Assets.xcassets */; };
D6BDB3A31BE768030009E292 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D6BDB3A11BE768030009E292 /* LaunchScreen.storyboard */; };
D6BDB3B11BE772360009E292 /* KNSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BDB3B01BE772360009E292 /* KNSwitcher.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
7E1424751E7FB0630078B9D7 /* KNSwitcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KNSwitcher.swift; sourceTree = "<group>"; };
D6BDB3951BE768030009E292 /* SwitcherExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwitcherExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
D6BDB3981BE768030009E292 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D6BDB39A1BE768030009E292 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
D6BDB39D1BE768030009E292 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
D6BDB39F1BE768030009E292 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D6BDB3A21BE768030009E292 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
D6BDB3A41BE768030009E292 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D6BDB3B01BE772360009E292 /* KNSwitcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KNSwitcher.swift; path = ../KNSwitcher.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -37,6 +37,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
7E1424741E7FB0630078B9D7 /* KNSwitcher */ = {
isa = PBXGroup;
children = (
7E1424751E7FB0630078B9D7 /* KNSwitcher.swift */,
);
path = KNSwitcher;
sourceTree = "<group>";
};
D6BDB38C1BE768020009E292 = {
isa = PBXGroup;
children = (
Expand All @@ -62,7 +70,7 @@
D6BDB39F1BE768030009E292 /* Assets.xcassets */,
D6BDB3A11BE768030009E292 /* LaunchScreen.storyboard */,
D6BDB3A41BE768030009E292 /* Info.plist */,
D6BDB3B01BE772360009E292 /* KNSwitcher.swift */,
7E1424741E7FB0630078B9D7 /* KNSwitcher */,
);
path = SwitcherExample;
sourceTree = "<group>";
Expand Down Expand Up @@ -139,8 +147,8 @@
buildActionMask = 2147483647;
files = (
D6BDB39B1BE768030009E292 /* ViewController.swift in Sources */,
7E1424761E7FB0630078B9D7 /* KNSwitcher.swift in Sources */,
D6BDB3991BE768030009E292 /* AppDelegate.swift in Sources */,
D6BDB3B11BE772360009E292 /* KNSwitcher.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
141 changes: 141 additions & 0 deletions SwitcherExample/KNSwitcher/KNSwitcher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//
// Switcher.swift
// SwitcherExample
//
// Created by Khoi Nguyen Nguyen on 11/2/15.
// Copyright © 2015 Khoi Nguyen Nguyen. All rights reserved.
//

import UIKit

protocol KNSwitcherChangeValueDelegate {
func switcherDidChangeValue(switcher: KNSwitcher,value: Bool)
}

class KNSwitcher: UIView {

var button: UIButton!
var buttonLeftConstraint: NSLayoutConstraint!
var delegate: KNSwitcherChangeValueDelegate?

@IBInspectable var on: Bool = false
@IBInspectable var originalImage:UIImage?
@IBInspectable var selectedImage:UIImage?
@IBInspectable var selectedColor:UIColor = UIColor(red: 126/255.0, green: 134/255.0, blue: 249/255.0, alpha: 1)
@IBInspectable var originalColor:UIColor = UIColor(red: 243/255.0, green: 229/255.0, blue: 211/255.0, alpha: 1)

private var offCenterPosition: CGFloat!
private var onCenterPosition: CGFloat!

init(frame: CGRect, on: Bool) {
super.init(frame: frame)
self.on = on
commonInit()
}

override func awakeFromNib() {
commonInit()
}

private func commonInit() {
button = UIButton(type: .custom)
self.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(switcherButtonTouch(_:)), for: UIControlEvents.touchUpInside)
button.setImage(originalImage, for: .normal)
button.setImage(selectedImage, for: .selected)
offCenterPosition = self.bounds.height * 0.1
onCenterPosition = self.bounds.width - (self.bounds.height * 0.9)

if on == true {
self.button.backgroundColor = selectedColor
} else {
self.button.backgroundColor = originalColor
}

if self.backgroundColor == nil {
self.backgroundColor = .white
}
initLayout()
animationSwitcherButton()
}

override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = self.bounds.height / 2
self.clipsToBounds = true
button.layer.cornerRadius = button.bounds.height / 2
}

private func initLayout() {
button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
buttonLeftConstraint = button.leftAnchor.constraint(equalTo: self.leftAnchor)
buttonLeftConstraint.isActive = true
button.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.8).isActive = true
button.widthAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true
}

func setImages(onImage:UIImage? , offImage :UIImage?) {
button.setImage(offImage, for: .normal)
button.setImage(onImage, for: .selected)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func switcherButtonTouch(_ sender: AnyObject) {
on = !on
animationSwitcherButton()
delegate?.switcherDidChangeValue(switcher: self, value: on)
}

func animationSwitcherButton() {
if on == true {
// Rotate animation
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = -CGFloat(M_PI)
rotateAnimation.toValue = 0.0
rotateAnimation.duration = 0.45
rotateAnimation.isCumulative = false;
self.button.layer.add(rotateAnimation, forKey: "rotate")

// Translation animation
UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: { () -> Void in
self.button.isSelected = true
self.buttonLeftConstraint.constant = self.onCenterPosition
self.layoutIfNeeded()
self.button.backgroundColor = self.selectedColor
}, completion: { (finish:Bool) -> Void in
self.button.layer.shadowOffset = CGSize(width: 0, height: 0.2)
self.button.layer.shadowOpacity = 0.3
self.button.layer.shadowRadius = self.offCenterPosition
self.button.layer.cornerRadius = self.button.frame.height / 2
self.button.layer.shadowPath = UIBezierPath(roundedRect: self.button.layer.bounds, cornerRadius: self.button.frame.height / 2).cgPath
})
} else {
// Clear Shadow
self.button.layer.shadowOffset = CGSize.zero
self.button.layer.shadowOpacity = 0
self.button.layer.shadowRadius = self.button.frame.height / 2
self.button.layer.cornerRadius = self.button.frame.height / 2
self.button.layer.shadowPath = nil

// Rotate animation
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = -CGFloat(M_PI)
rotateAnimation.duration = 0.45
rotateAnimation.isCumulative = false;
self.button.layer.add(rotateAnimation, forKey: "rotate")

UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: { () -> Void in
self.button.isSelected = false
self.buttonLeftConstraint.constant = self.offCenterPosition
self.layoutIfNeeded()
self.button.backgroundColor = self.originalColor
}, completion: { (finish:Bool) -> Void in
})
}
}
}

0 comments on commit 1c80610

Please sign in to comment.