Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jason-shen/5035f790ec22904c10cc2b288f6c51d0 to your computer and use it in GitHub Desktop.
Save jason-shen/5035f790ec22904c10cc2b288f6c51d0 to your computer and use it in GitHub Desktop.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
let titleLabel = UILabel()
let captionLabel = UILabel()
let coverImageView = UIImageView()
let backgroundImageView = UIImageView()
let descriptionLabel = UILabel()
let closeButton = UIButton()
let cardView = UIView()
override func loadView() {
let view = UIView()
view.backgroundColor = .white
titleLabel.frame = CGRect(x: 16, y: 16, width: 272, height: 38)
titleLabel.text = "Learn Swift 4"
titleLabel.textColor = .white
titleLabel.font = UIFont.systemFont(ofSize: 32, weight: .semibold)
captionLabel.frame = CGRect(x: 16, y: 204, width: 272, height: 40)
captionLabel.text = "Design directly in Playground"
captionLabel.textColor = .white
coverImageView.frame = CGRect(x: 0, y: 0, width: 300, height: 250)
coverImageView.contentMode = .scaleAspectFill
coverImageView.image = UIImage(named: "Cover.jpg")
coverImageView.clipsToBounds = true
coverImageView.layer.cornerRadius = 14
backgroundImageView.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
backgroundImageView.image = #imageLiteral(resourceName: "Chapters [email protected]")
descriptionLabel.frame = CGRect(x: 20, y: 448, width: 335, height: 132)
descriptionLabel.text = "Three years ago, Apple completely revamped their design language for the modern users. It is now much simpler, allowing designers to focus on animation and function rather than intricate visual details."
descriptionLabel.textColor = .black
descriptionLabel.numberOfLines = 10
descriptionLabel.alpha = 0
closeButton.frame = CGRect(x: 328, y: 20, width: 28, height: 28)
closeButton.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.5)
closeButton.layer.cornerRadius = 14
closeButton.alpha = 0
closeButton.setImage(#imageLiteral(resourceName: "[email protected]"), for: .normal)
closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
let tap = UITapGestureRecognizer(target: self, action: #selector(cardViewTapped))
cardView.addGestureRecognizer(tap)
cardView.isUserInteractionEnabled = true
cardView.frame = CGRect(x: 20, y: 255, width: 300, height: 250)
cardView.backgroundColor = .white
cardView.layer.cornerRadius = 14
cardView.layer.shadowOpacity = 0.25
cardView.layer.shadowOffset = CGSize(width: 0, height: 10)
cardView.layer.shadowRadius = 10
view.addSubview(backgroundImageView)
view.addSubview(cardView)
cardView.addSubview(coverImageView)
cardView.addSubview(closeButton)
cardView.addSubview(titleLabel)
cardView.addSubview(captionLabel)
cardView.addSubview(descriptionLabel)
self.view = view
}
@objc
func cardViewTapped() {
let animator = UIViewPropertyAnimator(duration: 0.7, dampingRatio: 0.7) {
self.cardView.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
self.cardView.layer.cornerRadius = 0
self.coverImageView.frame = CGRect(x: 0, y: 0, width: 375, height: 420)
self.coverImageView.layer.cornerRadius = 0
self.titleLabel.frame = CGRect(x: 20, y: 20, width: 374, height: 38)
self.captionLabel.frame = CGRect(x: 20, y: 370, width: 272, height: 40)
self.descriptionLabel.alpha = 1
self.closeButton.alpha = 1
}
animator.startAnimation()
}
@objc
func closeButtonTapped() {
let animator = UIViewPropertyAnimator(duration: 0.7, dampingRatio: 0.7) {
self.cardView.frame = CGRect(x: 20, y: 255, width: 300, height: 250)
self.cardView.layer.cornerRadius = 14
self.coverImageView.frame = CGRect(x: 0, y: 0, width: 300, height: 250)
self.coverImageView.layer.cornerRadius = 14
self.titleLabel.frame = CGRect(x: 16, y: 16, width: 272, height: 38)
self.captionLabel.frame = CGRect(x: 16, y: 204, width: 272, height: 40)
self.descriptionLabel.alpha = 0
self.closeButton.alpha = 0
}
animator.startAnimation()
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment