You might be aware that Swift’s Playgrounds are a great way to write and test code quickly and easily.
Did you also know you can use liveView and gestures to create a fully functioning app?
The end product
You’ll be able to drag and drop a shape on the background. Like (you know) in a very simple App. However — this will be happening in Playgrounds.
Setting up the Playground
PlaygroundSupport has all you need to make the most of your playground experience.
By this I mean you can add
which enables a collection of great features. One of these is to (assuming we have a
MyViewController() UIViewController (or even UIView) instantiated somewhere in the project).
PlaygroundPage.current.liveView = MyViewController()
Using a Gesture
For this tutorial I’ve picked a pan gesture because it’s, you know, fun.
Dragging around a UIView — why wouldn’t you?
To do so we set up the pan recogniser in
viewDidLoad() with the following couple of lines
which calls our method to handle the pan:
And.. that’s it.
You can click on the view and drag it around the screen!
Setting up the reset of the project
If you would want a guide to setting up the whole project, I can tell you that using loadView() is the right way to go, but for a less subtle hint here is the whole code for a copy-pasta opportunity for you. Of course you can also download the code using the Gist below: