Skip to content

Instantly share code, notes, and snippets.

@SAllen0400
Last active February 7, 2017 02:37
Show Gist options
  • Save SAllen0400/5804d0d5bc75975913ac88be9d9d5310 to your computer and use it in GitHub Desktop.
Save SAllen0400/5804d0d5bc75975913ac88be9d9d5310 to your computer and use it in GitHub Desktop.
Custom Switch
import UIKit
class SASwitch: UIView {
var backgroundView: UIView!
var beforeButton: UIButton!
var afterButton: UIButton!
var buttonWindow: UIView!
var beforeLabel: UILabel!
var afterLabel: UILabel!
let selectedColor = Colors.white
let unselectedColor = Colors.mediumWhite
var isAfter: Bool!
override func drawRect(rect: CGRect) {
backgroundView = UIView()
backgroundView.frame = bounds
backgroundView.backgroundColor = Colors.darkGrey
backgroundView.layer.cornerRadius = backgroundView.frame.size.height/2
addSubview(backgroundView)
// Setup the Sliding Window
buttonWindow = UIView()
buttonWindow.frame = CGRectMake(0.0, 0.0, bounds.size.width/2, bounds.size.height)
buttonWindow.backgroundColor = Colors.brightOrange
buttonWindow.layer.cornerRadius = bounds.size.height/2
addSubview(buttonWindow)
// Setup the Buttons
beforeButton = UIButton()
beforeButton.frame = CGRectMake(0.0, 0.0, bounds.size.width / 2, bounds.size.height)
beforeButton.backgroundColor = UIColor.clearColor()
beforeButton.enabled = false
beforeButton.addTarget(self, action: #selector(BMSwitch.toggleSwitch(_:)), forControlEvents: UIControlEvents.TouchUpInside)
addSubview(beforeButton)
afterButton = UIButton()
afterButton.frame = CGRectMake(bounds.size.width / 2, 0.0, bounds.size.width / 2, bounds.size.height)
afterButton.backgroundColor = UIColor.clearColor()
afterButton.enabled = true
afterButton.addTarget(self, action: #selector(BMSwitch.toggleSwitch(_:)), forControlEvents: UIControlEvents.TouchUpInside)
addSubview(afterButton)
// Setup the Labels
beforeLabel = UILabel()
beforeLabel.frame = CGRectMake(0.0, (bounds.size.height / 2) - 25.0, bounds.size.width / 2, 50.0)
beforeLabel.alpha = 1.0
beforeLabel.text = "BEFORE"
beforeLabel.textAlignment = NSTextAlignment.Center
beforeLabel.textColor = Colors.white
beforeLabel.font = UIFont(name: Fonts.avenirNextDemiBoldItalic, size: 11.0)
beforeButton.addSubview(beforeLabel)
afterLabel = UILabel()
afterLabel.frame = CGRectMake(0.0, (bounds.size.height / 2) - 25.0, bounds.size.width / 2, 50.0)
afterLabel.alpha = 1.0
afterLabel.text = "AFTER"
afterLabel.textAlignment = NSTextAlignment.Center
afterLabel.textColor = unselectedColor
afterLabel.font = UIFont(name: Fonts.avenirNextDemiBoldItalic, size: 11.0)
afterButton.addSubview(afterLabel)
isAfter = false
}
func toggleSwitch(sender: UIButton) {
beforeOrAfter(!isAfter)
}
func beforeOrAfter(before: Bool){
if before == isAfter {
return
}
isAfter = before
UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 14.0, options: .CurveEaseOut, animations: { () -> Void in
self.buttonWindow.frame.origin.x += self.frame.size.width / 2 * (before ? 1 : -1)
}, completion: nil)
animateLabel(self.afterLabel, toColor: (before ? selectedColor : unselectedColor))
animateLabel(self.beforeLabel, toColor: (before ? unselectedColor : selectedColor))
beforeButton.enabled = !beforeButton.enabled
afterButton.enabled = !afterButton.enabled
}
private func animateLabel(label : UILabel!, toColor : UIColor){
UIView.transitionWithView(label, duration: 0.4, options: .CurveEaseOut, animations: { () -> Void in
label.textColor = toColor
}, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment