Created
November 8, 2020 19:56
-
-
Save anuragajwani/bc74f111d8dfad0b0ed7590a37178b1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class MoveInOutViewController: UIViewController { | |
private weak var cardView: UIView! | |
private var cardViewHiddenConstraints: [NSLayoutConstraint]! | |
private var cardViewPresentConstraints: [NSLayoutConstraint]! | |
override func loadView() { | |
self.view = UIView() | |
self.view.backgroundColor = .white | |
self.loadViews() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
self.showCardView() | |
} | |
private func loadViews() { | |
let cardView = UIView() | |
cardView.translatesAutoresizingMaskIntoConstraints = false | |
cardView.backgroundColor = .white | |
cardView.layer.masksToBounds = false | |
cardView.layer.cornerRadius = 5 | |
cardView.layer.borderColor = UIColor.lightGray.cgColor | |
cardView.layer.borderWidth = 1.5 | |
cardView.layer.shadowColor = UIColor.black.cgColor | |
cardView.layer.shadowRadius = 3 | |
cardView.layer.shadowOpacity = 0.8 | |
cardView.layer.shadowOffset = CGSize(width: 0, height: 3.0) | |
self.view.addSubview(cardView) | |
self.cardView = cardView | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.text = "Buy me" | |
self.cardView.addSubview(label) | |
let button = UIButton() | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.setTitle("Nah I'm good", for: .normal) | |
button.addTarget(self, action: #selector(self.dismissCardView), for: .touchUpInside) | |
button.setTitleColor(.systemBlue, for: .normal) | |
self.cardView.addSubview(button) | |
self.cardViewHiddenConstraints = [ | |
cardView.leftAnchor.constraint(equalTo: self.view.rightAnchor) | |
] | |
self.cardViewPresentConstraints = [ | |
cardView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10) | |
] | |
NSLayoutConstraint.activate([ | |
cardView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5), | |
cardView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 0.4), | |
cardView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor), | |
label.centerXAnchor.constraint(equalTo: cardView.centerXAnchor), | |
label.centerYAnchor.constraint(equalTo: cardView.centerYAnchor), | |
button.centerXAnchor.constraint(equalTo: cardView.centerXAnchor), | |
button.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 5) | |
] + self.cardViewHiddenConstraints) | |
} | |
private func showCardView() { | |
NSLayoutConstraint.deactivate(self.cardViewHiddenConstraints) | |
UIViewPropertyAnimator.runningPropertyAnimator( | |
withDuration: 1.0, | |
delay: 0.0, | |
options: .curveEaseOut, | |
animations: { | |
NSLayoutConstraint.activate(self.cardViewPresentConstraints) | |
self.view.layoutIfNeeded() | |
}, | |
completion: nil) | |
} | |
@objc private func dismissCardView() { | |
NSLayoutConstraint.deactivate(self.cardViewPresentConstraints) | |
UIViewPropertyAnimator.runningPropertyAnimator( | |
withDuration: 1.0, | |
delay: 2.0, | |
options: .curveEaseOut, | |
animations: { | |
NSLayoutConstraint.activate(self.cardViewHiddenConstraints) | |
self.view.layoutIfNeeded() | |
}, | |
completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment