SAIInputView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // SAIInputView.swift
  3. // SAIInputBar
  4. //
  5. // Created by SAGESSE on 7/23/16.
  6. // Copyright © 2016-2017 SAGESSE. All rights reserved.
  7. //
  8. import UIKit
  9. internal class SAIInputView: UIView {
  10. override var intrinsicContentSize: CGSize {
  11. if let size = _cacheContentSize {
  12. return size
  13. }
  14. _cacheContentSize = _inputView?.intrinsicContentSize
  15. return _cacheContentSize ?? .zero
  16. }
  17. override func invalidateIntrinsicContentSize() {
  18. _cacheContentSize = nil
  19. _inputView?.invalidateIntrinsicContentSize()
  20. super.invalidateIntrinsicContentSize()
  21. }
  22. override func layoutSubviews() {
  23. if _cacheBounds?.width != bounds.width {
  24. _cacheBounds = bounds
  25. invalidateIntrinsicContentSize()
  26. }
  27. super.layoutSubviews()
  28. }
  29. func updateInputMode(_ newMode: SAIInputMode, oldMode: SAIInputMode, animated: Bool) {
  30. _inputMode = newMode
  31. switch newMode {
  32. case .selecting(let view):
  33. guard view != _inputView else {
  34. break
  35. }
  36. view.translatesAutoresizingMaskIntoConstraints = false
  37. addSubview(view)
  38. let viewcs = [
  39. _SAInputLayoutConstraintMake(view, .top, .equal, self, .top),
  40. _SAInputLayoutConstraintMake(view, .left, .equal, self, .left),
  41. _SAInputLayoutConstraintMake(view, .right, .equal, self, .right),
  42. ]
  43. addConstraints(viewcs)
  44. view.layoutIfNeeded()
  45. view.frame = CGRect(origin: .zero, size: view.frame.size)
  46. //setNeedsLayout()
  47. //layoutIfNeeded()
  48. if let oview = _inputView, let oviewcs = _inputViewConstraints, oldMode.isSelecting {
  49. oview.transform = CGAffineTransform(translationX: 0, y: 0)
  50. view.transform = CGAffineTransform(translationX: 0, y: view.frame.height)
  51. UIView.animate(withDuration: _SAInputDefaultAnimateDuration, animations: {
  52. UIView.setAnimationCurve(_SAInputDefaultAnimateCurve)
  53. oview.transform = CGAffineTransform(translationX: 0, y: oview.frame.height)
  54. view.transform = CGAffineTransform(translationX: 0, y: 0)
  55. }, completion: { b in
  56. guard self._inputView !== oview else {
  57. return
  58. }
  59. self.removeConstraints(oviewcs)
  60. oview.removeFromSuperview()
  61. oview.transform = CGAffineTransform(translationX: 0, y: 0)
  62. })
  63. }
  64. // update input view
  65. _inputView = view
  66. _inputViewConstraints = viewcs
  67. default:
  68. if let oview = _inputView, let oviewcs = _inputViewConstraints {
  69. UIView.animate(withDuration: _SAInputDefaultAnimateDuration, animations: {
  70. UIView.setAnimationCurve(_SAInputDefaultAnimateCurve)
  71. oview.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 0)
  72. }, completion: { b in
  73. if self._inputMode.isSelecting && self._inputView === oview {
  74. return // ignore
  75. }
  76. self.removeConstraints(oviewcs)
  77. oview.removeFromSuperview()
  78. self._inputView = nil
  79. self._inputViewConstraints = nil
  80. })
  81. }
  82. break
  83. }
  84. invalidateIntrinsicContentSize()
  85. }
  86. private var _cacheBounds: CGRect?
  87. private var _cacheContentSize: CGSize?
  88. private var _inputMode: SAIInputMode = .none
  89. private var _inputView: UIView?
  90. private var _inputViewConstraints: [NSLayoutConstraint]?
  91. }