Skip to content

Instantly share code, notes, and snippets.

@RamonGilabert
Created August 7, 2017 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RamonGilabert/7c4e336cfb823de0f4f37c8bf0752438 to your computer and use it in GitHub Desktop.
Save RamonGilabert/7c4e336cfb823de0f4f37c8bf0752438 to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
let scale = CGAffineTransform.init(scaleX: 0.9, y: 0.9)
let rotation = CGAffineTransform.init(rotationAngle: .pi / 2)
let blue = UIColor(red: 0.25, green: 0.85, blue: 1, alpha: 1)
let red = UIColor(red: 1, green: 0.55, blue: 0.55, alpha: 1)
var state = 0
override func loadView() {
let background = UIView()
background.backgroundColor = .white
let rectangle = UIView()
rectangle.frame = CGRect(x: 92.5, y: 230, width: 170, height: 170)
values(rectangle, true)
let gesture = UITapGestureRecognizer()
gesture.addTarget(self, action: #selector(tapGesture))
rectangle.addGestureRecognizer(gesture)
background.addSubview(rectangle)
view = background
}
@objc func tapGesture(gesture: UITapGestureRecognizer) {
guard let view = gesture.view else { return }
let condition = state == 1
let spring = UISpringTimingParameters(mass: 3, stiffness: 800, damping: 40, initialVelocity: .zero)
let animation = UIViewPropertyAnimator(duration: 0, timingParameters: spring)
animation.addAnimations {
self.values(view, condition)
}
state = condition ? 0 : 1
animation.startAnimation()
}
func values(_ view: UIView, _ initial: Bool) {
view.transform = initial ? .identity : rotation.concatenating(scale)
view.backgroundColor = initial ? blue : red
view.layer.cornerRadius = initial ? 60 : 85
}
}
PlaygroundPage.current.liveView = ViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment