Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.