Skip to content

Instantly share code, notes, and snippets.

@RamonGilabert
Last active August 24, 2018 22:47
Show Gist options
  • Save RamonGilabert/521010f66467897546b78375825f35e7 to your computer and use it in GitHub Desktop.
Save RamonGilabert/521010f66467897546b78375825f35e7 to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
class Controller: UIViewController {
static let red: UIColor = UIColor(red: 219/255, green: 10/255, blue: 64/255, alpha: 1)
static let size: CGSize = CGSize(width: 240, height: 240)
static let duration: TimeInterval = 2.5
static let transform: CGFloat = 0.37
lazy var container: UIView = {
let view = UIView()
view.frame.size = Controller.size
view.backgroundColor = .clear
return view
}()
lazy var outside: UIView = {
let view = UIView()
view.frame.size = Controller.size
view.layer.cornerRadius = view.frame.size.width / 2
view.transform = CGAffineTransform.init(scaleX: Controller.transform,
y: Controller.transform)
view.alpha = 0.5
return view
}()
lazy var inside: UIView = {
let view = UIView()
view.frame.size = CGSize(width: Controller.size.width / 2.5,
height: Controller.size.height / 2.5)
view.layer.cornerRadius = view.frame.size.width / 2
return view
}()
override func loadView() {
let background = UIView() // Because Playgrounds.
background.backgroundColor = .white
view = background
}
override func viewDidLoad() {
super.viewDidLoad()
for element in [outside, inside] {
element.backgroundColor = Controller.red
element.center = container.center
container.addSubview(element)
}
view.addSubview(container)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animate()
}
func animate() {
UIView.animate(withDuration: Controller.duration,
delay: 0,
options: [.repeat, .curveEaseInOut],
animations: {
self.outside.transform = .identity
self.outside.alpha = 0
}, completion: nil)
UIView.animate(withDuration: Controller.duration / 2,
delay: 0,
options: [.repeat, .autoreverse, .curveEaseInOut],
animations: {
self.inside.transform = .init(scaleX: 1.2, y: 1.2)
}, completion: nil)
}
}
let controller = Controller()
controller.preferredContentSize = Controller.size
PlaygroundPage.current.liveView = controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment