Skip to content

Instantly share code, notes, and snippets.

@spacedrabbit
Created January 23, 2017 23:30
Show Gist options
  • Save spacedrabbit/c0c0282c4879d72c804a0a1aa0e69d49 to your computer and use it in GitHub Desktop.
Save spacedrabbit/c0c0282c4879d72c804a0a1aa0e69d49 to your computer and use it in GitHub Desktop.
import UIKit
import SnapKit
// Stage 2 adds a scale transform allow with a delay factor animation to give the impression of "picking up" and "placing down" a view
class TouchAnimatorViewController: UIViewController {
var animator: UIViewPropertyAnimator? = nil
let squareSize = CGSize(width: 100.0, height: 100.0)
// MARK: - View LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
setupViewHierarchy()
configureConstraints()
}
// MARK: - Setup
private func configureConstraints() {
darkBlueView.snp.makeConstraints{ view in
view.center.equalToSuperview()
view.size.equalTo(squareSize)
}
}
private func setupViewHierarchy() {
self.view.backgroundColor = .white
self.view.isUserInteractionEnabled = true
view.addSubview(darkBlueView)
}
// MARK: - Movement
internal func move(view: UIView, to point: CGPoint) {
view.snp.remakeConstraints { (view) in
view.center.equalTo(point)
view.size.equalTo(squareSize)
}
animator = UIViewPropertyAnimator(duration: 0.8, dampingRatio: 0.5, animations: {
self.darkBlueView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
})
animator?.addAnimations({
self.view.layoutIfNeeded()
}, delayFactor: 0.15)
animator?.addAnimations({
self.darkBlueView.transform = CGAffineTransform.identity
}, delayFactor: 0.85)
animator?.startAnimation()
}
// MARK: - Tracking Touches
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
move(view: darkBlueView, to: touch.location(in: view))
}
// MARK: - Views
internal lazy var darkBlueView: UIView = {
let view: UIView = UIView()
view.backgroundColor = Colors.darkBlue
return view
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment