Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import UIKit
  2.  
  3. /// The container that lets you to flip two views.
  4. class FlipView: UIView {
  5.  
  6. /// Describes the side from which the view will flip.
  7. enum Side {
  8.  
  9. /// Flips from the left side.
  10. case left
  11.  
  12. /// Flips from the right side.
  13. case right
  14.  
  15. /// Flips from the top side.
  16. case top
  17.  
  18. /// Flips from the bottom side.
  19. case bottom
  20.  
  21. /// The inversed flip side.
  22. var inversed: Side {
  23. switch self {
  24. case .left:
  25. return .right
  26. case .right:
  27. return .left
  28. case .top:
  29. return .bottom
  30. case .bottom:
  31. return .top
  32. }
  33. }
  34.  
  35. /// The animation option.
  36. var animationOption: AnimationOptions {
  37. switch self {
  38. case .left:
  39. return .transitionFlipFromLeft
  40. case .right:
  41. return .transitionFlipFromRight
  42. case .top:
  43. return .transitionFlipFromTop
  44. case .bottom:
  45. return .transitionFlipFromBottom
  46. }
  47. }
  48.  
  49. }
  50.  
  51. /// The front view in the container.
  52. var frontView = UIView() {
  53. didSet {
  54. oldValue.removeFromSuperview()
  55. configure(frontView)
  56. }
  57. }
  58.  
  59. /// The back view in the container.
  60. var backView = UIView() {
  61. didSet {
  62. oldValue.removeFromSuperview()
  63. configure(backView)
  64. }
  65. }
  66.  
  67. /// The transition duration.
  68. var duration = 0.5
  69.  
  70. /// The flag that indicates that the flip side will be inverted after showing the back side.
  71. var hasInversibleSide = false
  72.  
  73. /// The flag that indicates which view to show.
  74. private var isFlipped = false
  75.  
  76. /// Initializes the container with a frame.
  77. ///
  78. /// - Parameter frame: The container frame.
  79. override init(frame: CGRect) {
  80. super.init(frame: frame)
  81.  
  82. configure(frontView)
  83. configure(backView)
  84. }
  85.  
  86. /// From `NSCoding` protocol.
  87. required init?(coder aDecoder: NSCoder) {
  88. fatalError("init(coder:) has not been implemented")
  89. }
  90.  
  91. /// Configures a view in the container.
  92. ///
  93. /// - Parameter view: A view in the container.
  94. private func configure(_ view: UIView) {
  95.  
  96. // Add a view to the container.
  97. addSubview(view)
  98.  
  99. // Bring the front view to front of the container.
  100. bringSubviewToFront(frontView)
  101.  
  102. view.translatesAutoresizingMaskIntoConstraints = false
  103.  
  104. // Make a view size equal to the container size.
  105. NSLayoutConstraint.activate([
  106. view.widthAnchor.constraint(equalTo: widthAnchor),
  107. view.heightAnchor.constraint(equalTo: heightAnchor),
  108. view.centerXAnchor.constraint(equalTo: centerXAnchor),
  109. view.centerYAnchor.constraint(equalTo: centerYAnchor),
  110. ])
  111. }
  112.  
  113. /// Flips the front view.
  114. ///
  115. /// - Parameter side: The side from which the view will flip.
  116. func flip(from side: Side) {
  117.  
  118. // Choose views for transition.
  119. let from = isFlipped ? backView : frontView
  120. let to = isFlipped ? frontView : backView
  121.  
  122. // Inverse the flip side if the view is flipped and the flip side is inversible.
  123. let side = isFlipped && hasInversibleSide ? side.inversed : side
  124.  
  125. // Flip the front view.
  126. UIView.transition(
  127. from: from,
  128. to: to,
  129. duration: duration,
  130. options: [side.animationOption, .showHideTransitionViews]) { [unowned self] _ in
  131. self.isFlipped = !self.isFlipped
  132. }
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement