InputBarSendButton.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // InputBarSendButton.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 8/18/17.
  26. //
  27. import UIKit
  28. open class InputBarSendButton: InputBarButtonItem {
  29. /// A flag indicating the animation state of the `InputBarSendButton`
  30. open private(set) var isAnimating: Bool = false
  31. /// Accessor to modify the color of the activity view
  32. open var activityViewColor: UIColor! {
  33. get {
  34. return activityView.color
  35. }
  36. set {
  37. activityView.color = newValue
  38. }
  39. }
  40. private let activityView: UIActivityIndicatorView = {
  41. let view = UIActivityIndicatorView(style: .gray)
  42. view.isUserInteractionEnabled = false
  43. view.isHidden = true
  44. return view
  45. }()
  46. public convenience init() {
  47. self.init(frame: .zero)
  48. }
  49. public override init(frame: CGRect) {
  50. super.init(frame: frame)
  51. setupSendButton()
  52. }
  53. required public init?(coder aDecoder: NSCoder) {
  54. super.init(coder: aDecoder)
  55. setupSendButton()
  56. }
  57. private func setupSendButton() {
  58. addSubview(activityView)
  59. }
  60. open override func layoutSubviews() {
  61. super.layoutSubviews()
  62. activityView.frame = bounds
  63. }
  64. /// Starts the animation of the activity view, hiding other elements
  65. open func startAnimating() {
  66. guard !isAnimating else { return }
  67. defer { isAnimating = true }
  68. activityView.startAnimating()
  69. activityView.isHidden = false
  70. // Setting isHidden doesn't hide the elements
  71. titleLabel?.alpha = 0
  72. imageView?.layer.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
  73. }
  74. /// Stops the animation of the activity view, shows other elements
  75. open func stopAnimating() {
  76. guard isAnimating else { return }
  77. defer { isAnimating = false }
  78. activityView.stopAnimating()
  79. activityView.isHidden = true
  80. titleLabel?.alpha = 1
  81. imageView?.layer.transform = CATransform3DIdentity
  82. }
  83. }