PasswordToggleVisibilityView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // PasswordToggleVisibilityView.swift
  3. // CockFight
  4. //
  5. // Created by Visoth Phon on 3/20/21.
  6. //
  7. import Foundation
  8. import UIKit
  9. protocol PasswordToggleVisibilityDelegate: class {
  10. func viewWasToggled(_ passwordToggleVisibilityView: PasswordToggleVisibilityView, isSelected selected: Bool)
  11. }
  12. class PasswordToggleVisibilityView: UIView {
  13. fileprivate let eyeOpenedImage: UIImage
  14. fileprivate let eyeClosedImage: UIImage
  15. fileprivate let checkmarkImage: UIImage
  16. fileprivate let eyeButton: UIButton
  17. fileprivate let checkmarkImageView: UIImageView
  18. weak var delegate: PasswordToggleVisibilityDelegate?
  19. enum EyeState {
  20. case open
  21. case closed
  22. }
  23. var eyeState: EyeState {
  24. set {
  25. eyeButton.isSelected = newValue == .open
  26. }
  27. get {
  28. return eyeButton.isSelected ? .open : .closed
  29. }
  30. }
  31. var checkmarkVisible: Bool {
  32. set {
  33. let isHidden = !newValue
  34. guard checkmarkImageView.isHidden != isHidden else {
  35. return
  36. }
  37. checkmarkImageView.isHidden = isHidden
  38. }
  39. get {
  40. return !checkmarkImageView.isHidden
  41. }
  42. }
  43. override var tintColor: UIColor! {
  44. didSet {
  45. eyeButton.tintColor = tintColor
  46. checkmarkImageView.tintColor = tintColor
  47. }
  48. }
  49. override init(frame: CGRect) {
  50. self.eyeOpenedImage = UIImage(named: "ic_eye_open", in: Bundle(for: PasswordToggleVisibilityView.self), compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
  51. self.eyeClosedImage = UIImage(named: "ic_eye_closed", in: Bundle(for: PasswordToggleVisibilityView.self), compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
  52. self.checkmarkImage = UIImage(named: "ic_password_checkmark", in: Bundle(for: PasswordToggleVisibilityView.self), compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
  53. self.eyeButton = UIButton(type: .custom)
  54. self.checkmarkImageView = UIImageView(image: self.checkmarkImage)
  55. super.init(frame: frame)
  56. setupViews()
  57. }
  58. required init?(coder aDecoder: NSCoder) {
  59. fatalError("Don't use init with coder.")
  60. }
  61. fileprivate func setupViews() {
  62. let padding: CGFloat = 10
  63. let buttonWidth = (frame.width / 2) - padding
  64. let buttonFrame = CGRect(x: buttonWidth + padding, y: 0, width: buttonWidth, height: frame.height)
  65. eyeButton.frame = buttonFrame
  66. eyeButton.backgroundColor = UIColor.clear
  67. eyeButton.adjustsImageWhenHighlighted = false
  68. eyeButton.setImage(self.eyeClosedImage, for: UIControl.State())
  69. eyeButton.setImage(self.eyeOpenedImage.withRenderingMode(.alwaysTemplate), for: .selected)
  70. eyeButton.addTarget(self, action: #selector(PasswordToggleVisibilityView.eyeButtonPressed(_:)), for: .touchUpInside)
  71. eyeButton.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  72. eyeButton.tintColor = self.tintColor
  73. self.addSubview(eyeButton)
  74. let checkmarkImageWidth = (frame.width / 2) - padding
  75. let checkmarkFrame = CGRect(x: padding, y: 0, width: checkmarkImageWidth, height: frame.height)
  76. checkmarkImageView.frame = checkmarkFrame
  77. checkmarkImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  78. checkmarkImageView.contentMode = .center
  79. checkmarkImageView.backgroundColor = UIColor.clear
  80. checkmarkImageView.tintColor = self.tintColor
  81. self.addSubview(checkmarkImageView)
  82. self.checkmarkImageView.isHidden = true
  83. }
  84. @objc func eyeButtonPressed(_ sender: AnyObject) {
  85. eyeButton.isSelected = !eyeButton.isSelected
  86. delegate?.viewWasToggled(self, isSelected: eyeButton.isSelected)
  87. }
  88. }