You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
1.9 KiB

  1. //
  2. // CheckBox.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 2020/01/02.
  6. // Copyright © 2020 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. import RxCocoa
  11. @objc protocol CheckBoxDelegate: class {
  12. func checkBox(_ checkBox: CheckBox, isSelected: Bool)
  13. }
  14. @IBDesignable
  15. public class CheckBox: UIView {
  16. private let xibName = "CheckBox"
  17. weak var delegate: CheckBoxDelegate?
  18. @IBOutlet private weak var checkMark: CheckMark!
  19. @IBOutlet private weak var button: UIButton!
  20. var isSelected: Bool = false {
  21. didSet {
  22. if isSelected {
  23. checkMark.animate(animationType: .check)
  24. } else {
  25. checkMark.animate(animationType: .remove)
  26. }
  27. delegate?.checkBox(self, isSelected: isSelected)
  28. }
  29. }
  30. @IBInspectable
  31. var borderWidth: CGFloat = 1 {
  32. didSet {
  33. update()
  34. }
  35. }
  36. @IBInspectable
  37. var borderColor: UIColor = .themeText {
  38. didSet {
  39. update()
  40. }
  41. }
  42. @IBInspectable
  43. var cornerRadius: CGFloat = 5 {
  44. didSet {
  45. update()
  46. }
  47. }
  48. override init(frame: CGRect) {
  49. super.init(frame: frame)
  50. commonInit()
  51. }
  52. required init?(coder: NSCoder) {
  53. super.init(coder: coder)
  54. commonInit()
  55. }
  56. override public func prepareForInterfaceBuilder() {
  57. update()
  58. }
  59. override public func awakeFromNib() {
  60. update()
  61. }
  62. private func commonInit() {
  63. guard let view = Bundle.main.loadNibNamed(
  64. xibName,
  65. owner: self,
  66. options: nil
  67. )?.first as? UIView else { return }
  68. view.frame = self.bounds
  69. view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  70. self.addSubview(view)
  71. checkMark.strokeWidth = 2
  72. update()
  73. }
  74. @IBAction private func touchCheck(_ sender: UIButton) {
  75. isSelected = !isSelected
  76. }
  77. private func update() {
  78. layer.cornerRadius = cornerRadius
  79. layer.borderWidth = borderWidth
  80. layer.borderColor = borderColor.cgColor
  81. }
  82. }