Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created May 16, 2020 08:44
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 stevencurtis/1856c8b2557d8da53e552c0367cf3475 to your computer and use it in GitHub Desktop.
Save stevencurtis/1856c8b2557d8da53e552c0367cf3475 to your computer and use it in GitHub Desktop.
fullplaygroundpan
import UIKit
import PlaygroundSupport
class MyViewController: UIViewController {
let clickableView = UIView()
override func loadView() {
let view = UIView()
view.backgroundColor = .lightGray
clickableView.backgroundColor = .blue
view.addSubview(clickableView)
clickableView.translatesAutoresizingMaskIntoConstraints = false
clickableView.widthAnchor.constraint(equalToConstant: 200).isActive = true
clickableView.heightAnchor.constraint(equalToConstant: 200).isActive = true
clickableView.centerYAnchor.constraint(equalTo:view.centerYAnchor).isActive = true
clickableView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
clickableView.addGestureRecognizer(pan)
}
@objc func handlePan(_ sender: UIPanGestureRecognizer) {
let targetView = sender.view!
let translation = sender.translation(in: view)
switch sender.state {
case .began,.changed, .ended:
targetView.center = CGPoint(x: targetView.center.x + translation.x
,y: targetView.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: view)
break
default:
break
}
}
}
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment