Created
January 23, 2017 23:30
-
-
Save spacedrabbit/c0c0282c4879d72c804a0a1aa0e69d49 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 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