Created
August 24, 2018 22:44
-
-
Save RamonGilabert/cb21a485a214ac8a8a461e38b39a1225 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 | |
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 | |
static let transform: CGFloat = 0.39 | |
static let delay: TimeInterval = Controller.duration * 2 | |
static let inside: TimeInterval = Controller.duration / 2 | |
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.6 | |
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 | |
view.transform = .identity | |
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() { | |
animation() | |
UIView.animate(withDuration: Controller.inside, | |
delay: 0, | |
options: [.repeat, .autoreverse, .curveEaseInOut], | |
animations: { | |
self.inside.transform = .init(scaleX: 1.2, y: 1.2) | |
}, completion: nil) | |
} | |
func animation() { | |
// There's a bug in Swift Playgrounds. | |
// But this could be achieved with a delay instead of a timer. | |
Timer.scheduledTimer(withTimeInterval: Controller.duration, repeats: false) { _ in | |
UIView.animate(withDuration: Controller.duration, | |
delay: 0, | |
options: .curveEaseIn, | |
animations: { | |
self.outside.transform = .identity | |
self.outside.alpha = 0 | |
}, completion: { _ in | |
self.outside.transform = CGAffineTransform.init(scaleX: Controller.transform, | |
y: Controller.transform) | |
self.outside.alpha = 0.7 | |
self.animation() | |
}) | |
} | |
} | |
} | |
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