Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- /// The container that lets you to flip two views.
- class FlipView: UIView {
- /// Describes the side from which the view will flip.
- enum Side {
- /// Flips from the left side.
- case left
- /// Flips from the right side.
- case right
- /// Flips from the top side.
- case top
- /// Flips from the bottom side.
- case bottom
- /// The inversed flip side.
- var inversed: Side {
- switch self {
- case .left:
- return .right
- case .right:
- return .left
- case .top:
- return .bottom
- case .bottom:
- return .top
- }
- }
- /// The animation option.
- var animationOption: AnimationOptions {
- switch self {
- case .left:
- return .transitionFlipFromLeft
- case .right:
- return .transitionFlipFromRight
- case .top:
- return .transitionFlipFromTop
- case .bottom:
- return .transitionFlipFromBottom
- }
- }
- }
- /// The front view in the container.
- var frontView = UIView() {
- didSet {
- oldValue.removeFromSuperview()
- configure(frontView)
- }
- }
- /// The back view in the container.
- var backView = UIView() {
- didSet {
- oldValue.removeFromSuperview()
- configure(backView)
- }
- }
- /// The transition duration.
- var duration = 0.5
- /// The flag that indicates that the flip side will be inverted after showing the back side.
- var hasInversibleSide = false
- /// The flag that indicates which view to show.
- private var isFlipped = false
- /// Initializes the container with a frame.
- ///
- /// - Parameter frame: The container frame.
- override init(frame: CGRect) {
- super.init(frame: frame)
- configure(frontView)
- configure(backView)
- }
- /// From `NSCoding` protocol.
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- /// Configures a view in the container.
- ///
- /// - Parameter view: A view in the container.
- private func configure(_ view: UIView) {
- // Add a view to the container.
- addSubview(view)
- // Bring the front view to front of the container.
- bringSubviewToFront(frontView)
- view.translatesAutoresizingMaskIntoConstraints = false
- // Make a view size equal to the container size.
- NSLayoutConstraint.activate([
- view.widthAnchor.constraint(equalTo: widthAnchor),
- view.heightAnchor.constraint(equalTo: heightAnchor),
- view.centerXAnchor.constraint(equalTo: centerXAnchor),
- view.centerYAnchor.constraint(equalTo: centerYAnchor),
- ])
- }
- /// Flips the front view.
- ///
- /// - Parameter side: The side from which the view will flip.
- func flip(from side: Side) {
- // Choose views for transition.
- let from = isFlipped ? backView : frontView
- let to = isFlipped ? frontView : backView
- // Inverse the flip side if the view is flipped and the flip side is inversible.
- let side = isFlipped && hasInversibleSide ? side.inversed : side
- // Flip the front view.
- UIView.transition(
- from: from,
- to: to,
- duration: duration,
- options: [side.animationOption, .showHideTransitionViews]) { [unowned self] _ in
- self.isFlipped = !self.isFlipped
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement