UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Setting up

Are you psychic? Of course not. But what if we could use our coding skills to make a game to fool your friends into thinking otherwise – while also learning some new techniques along the way?

In this project we're going to build a simple game that recreates the classic Zener test for extrasensory perception. Our game will show the user eight cards face down, and users need to tap the card that has a star on its flip side. Casual players will get it right 1 in 8 times, but you'll get it right every time. Magic!

Well, no. We'll be cheating, naturally, but even in this cheating I'm going to find new things to teach you. First, we're going to build a tiny watchOS app that silently taps your wrist when your finger moves over the star card. Then we're going to add 3D Touch support so that pressing hard on any card will automatically make it the star. Whichever technique you use is going to be enough to baffle your friends, although I hope you use your powers for good!

At the same time we'll also be learning about CAEmitterLayer, CAGradientLayer, @IBDesignable and @IBInspectable, as well as how to create a 3D card flip effect using the transition(with:) method.

I've left the Apple Watch and 3D Touch code until the end of the project, so at the very least you'll be able to work through the majority of the tutorial without needing special hardware. That being said, we'll be using one of Xcode's built-in iOS/watchOS templates to make the end result easier to reach.

Are you ready to take your first step into the Twilight Zone? Go ahead and launch Xcode, then create a new project. When Xcode asks you which template you want, please select watchOS then iOS App with WatchKit App. Set the target to be iPhone, the language to be Swift, then deselect Include Notification Scene. Finally, name it Project37 and click Next.

Once the project is created, please set it to support landscape left and right only; no portrait this time. You will also need to copy the image assets for this project into your project's asset catalog – you can find them all on GitHub at https://github.com/twostraws/HackingWithSwift. You'll also find a Content directory in that download; please add that to your project too, because it contains some music for later on.

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.