Skip to content

Instantly share code, notes, and snippets.

@spacedrabbit
Created January 23, 2017 22:28
Show Gist options
  • Save spacedrabbit/f795f5980c4f223a5230e96b8eb1c821 to your computer and use it in GitHub Desktop.
Save spacedrabbit/f795f5980c4f223a5230e96b8eb1c821 to your computer and use it in GitHub Desktop.
// Stage 1 simply moves the square's center point to the touch's location in self.view
// Tapping multiple times continues the same animation to the new point in the remaining animation duration
import UIKit
import SnapKit
class TouchAnimatorViewController: UIViewController {
let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeInOut, animations: nil)
let squareSize = CGSize(width: 100.0, height: 100.0)
override func viewDidLoad() {
super.viewDidLoad()
setupViewHierarchy()
configureConstraints()
}
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)
}
internal func move(view: UIView, to point: CGPoint) {
view.snp.remakeConstraints { (view) in
view.center.equalTo(point)
view.size.equalTo(squareSize)
}
animator.addAnimations {
self.view.layoutIfNeeded()
}
animator.startAnimation()
}
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