Created
May 16, 2020 08:44
-
-
Save stevencurtis/1856c8b2557d8da53e552c0367cf3475 to your computer and use it in GitHub Desktop.
fullplaygroundpan
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 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