|
| 1 | +// |
| 2 | +// Switcher.swift |
| 3 | +// SwitcherExample |
| 4 | +// |
| 5 | +// Created by Khoi Nguyen Nguyen on 11/2/15. |
| 6 | +// Copyright © 2015 Khoi Nguyen Nguyen. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +protocol KNSwitcherChangeValueDelegate { |
| 12 | + func switcherDidChangeValue(switcher: KNSwitcher,value: Bool) |
| 13 | +} |
| 14 | + |
| 15 | +class KNSwitcher: UIView { |
| 16 | + |
| 17 | + var button: UIButton! |
| 18 | + var buttonLeftConstraint: NSLayoutConstraint! |
| 19 | + var delegate: KNSwitcherChangeValueDelegate? |
| 20 | + |
| 21 | + @IBInspectable var on: Bool = false |
| 22 | + @IBInspectable var originalImage:UIImage? |
| 23 | + @IBInspectable var selectedImage:UIImage? |
| 24 | + @IBInspectable var selectedColor:UIColor = UIColor(red: 126/255.0, green: 134/255.0, blue: 249/255.0, alpha: 1) |
| 25 | + @IBInspectable var originalColor:UIColor = UIColor(red: 243/255.0, green: 229/255.0, blue: 211/255.0, alpha: 1) |
| 26 | + |
| 27 | + private var offCenterPosition: CGFloat! |
| 28 | + private var onCenterPosition: CGFloat! |
| 29 | + |
| 30 | + init(frame: CGRect, on: Bool) { |
| 31 | + super.init(frame: frame) |
| 32 | + self.on = on |
| 33 | + commonInit() |
| 34 | + } |
| 35 | + |
| 36 | + override func awakeFromNib() { |
| 37 | + commonInit() |
| 38 | + } |
| 39 | + |
| 40 | + private func commonInit() { |
| 41 | + button = UIButton(type: .custom) |
| 42 | + self.addSubview(button) |
| 43 | + button.translatesAutoresizingMaskIntoConstraints = false |
| 44 | + button.addTarget(self, action: #selector(switcherButtonTouch(_:)), for: UIControlEvents.touchUpInside) |
| 45 | + button.setImage(originalImage, for: .normal) |
| 46 | + button.setImage(selectedImage, for: .selected) |
| 47 | + offCenterPosition = self.bounds.height * 0.1 |
| 48 | + onCenterPosition = self.bounds.width - (self.bounds.height * 0.9) |
| 49 | + |
| 50 | + if on == true { |
| 51 | + self.button.backgroundColor = selectedColor |
| 52 | + } else { |
| 53 | + self.button.backgroundColor = originalColor |
| 54 | + } |
| 55 | + |
| 56 | + if self.backgroundColor == nil { |
| 57 | + self.backgroundColor = .white |
| 58 | + } |
| 59 | + initLayout() |
| 60 | + animationSwitcherButton() |
| 61 | + } |
| 62 | + |
| 63 | + override func layoutSubviews() { |
| 64 | + super.layoutSubviews() |
| 65 | + self.layer.cornerRadius = self.bounds.height / 2 |
| 66 | + self.clipsToBounds = true |
| 67 | + button.layer.cornerRadius = button.bounds.height / 2 |
| 68 | + } |
| 69 | + |
| 70 | + private func initLayout() { |
| 71 | + button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true |
| 72 | + buttonLeftConstraint = button.leftAnchor.constraint(equalTo: self.leftAnchor) |
| 73 | + buttonLeftConstraint.isActive = true |
| 74 | + button.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.8).isActive = true |
| 75 | + button.widthAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true |
| 76 | + } |
| 77 | + |
| 78 | + func setImages(onImage:UIImage? , offImage :UIImage?) { |
| 79 | + button.setImage(offImage, for: .normal) |
| 80 | + button.setImage(onImage, for: .selected) |
| 81 | + } |
| 82 | + |
| 83 | + required init?(coder aDecoder: NSCoder) { |
| 84 | + super.init(coder: aDecoder) |
| 85 | + } |
| 86 | + |
| 87 | + func switcherButtonTouch(_ sender: AnyObject) { |
| 88 | + on = !on |
| 89 | + animationSwitcherButton() |
| 90 | + delegate?.switcherDidChangeValue(switcher: self, value: on) |
| 91 | + } |
| 92 | + |
| 93 | + func animationSwitcherButton() { |
| 94 | + if on == true { |
| 95 | + // Rotate animation |
| 96 | + let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") |
| 97 | + rotateAnimation.fromValue = -CGFloat(M_PI) |
| 98 | + rotateAnimation.toValue = 0.0 |
| 99 | + rotateAnimation.duration = 0.45 |
| 100 | + rotateAnimation.isCumulative = false; |
| 101 | + self.button.layer.add(rotateAnimation, forKey: "rotate") |
| 102 | + |
| 103 | + // Translation animation |
| 104 | + UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: { () -> Void in |
| 105 | + self.button.isSelected = true |
| 106 | + self.buttonLeftConstraint.constant = self.onCenterPosition |
| 107 | + self.layoutIfNeeded() |
| 108 | + self.button.backgroundColor = self.selectedColor |
| 109 | + }, completion: { (finish:Bool) -> Void in |
| 110 | + self.button.layer.shadowOffset = CGSize(width: 0, height: 0.2) |
| 111 | + self.button.layer.shadowOpacity = 0.3 |
| 112 | + self.button.layer.shadowRadius = self.offCenterPosition |
| 113 | + self.button.layer.cornerRadius = self.button.frame.height / 2 |
| 114 | + self.button.layer.shadowPath = UIBezierPath(roundedRect: self.button.layer.bounds, cornerRadius: self.button.frame.height / 2).cgPath |
| 115 | + }) |
| 116 | + } else { |
| 117 | + // Clear Shadow |
| 118 | + self.button.layer.shadowOffset = CGSize.zero |
| 119 | + self.button.layer.shadowOpacity = 0 |
| 120 | + self.button.layer.shadowRadius = self.button.frame.height / 2 |
| 121 | + self.button.layer.cornerRadius = self.button.frame.height / 2 |
| 122 | + self.button.layer.shadowPath = nil |
| 123 | + |
| 124 | + // Rotate animation |
| 125 | + let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") |
| 126 | + rotateAnimation.fromValue = 0.0 |
| 127 | + rotateAnimation.toValue = -CGFloat(M_PI) |
| 128 | + rotateAnimation.duration = 0.45 |
| 129 | + rotateAnimation.isCumulative = false; |
| 130 | + self.button.layer.add(rotateAnimation, forKey: "rotate") |
| 131 | + |
| 132 | + UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: { () -> Void in |
| 133 | + self.button.isSelected = false |
| 134 | + self.buttonLeftConstraint.constant = self.offCenterPosition |
| 135 | + self.layoutIfNeeded() |
| 136 | + self.button.backgroundColor = self.originalColor |
| 137 | + }, completion: { (finish:Bool) -> Void in |
| 138 | + }) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments