NEW! Pre-order my latest book, Testing Swift! >>

< Previous: Setting up   Next: Switch, case, animate: animate(withDuration:) >

Preparing for action

Open Interface Builder with Main.storyboard and place a button on there with the title "Tap". Give it width 46 and height 44, with X:164 and Y:591. Don’t worry that we’re positioning things using an iPhone 8-sized screen – Auto Layout will automatically make it look great on iPads.

We need to add some Auto Layout constraints, so select the button in the document outline and Ctrl-drag diagonally from itself and to itself. The popup menu will offer "Width", "Height" and "Aspect Ratio"; please add Height.

We want our button to always stay near the bottom of the view controller, so Ctrl-drag from the button to the view directly below it and choose “Bottom Space to Safe Area.” Now Ctrl-drag the same way again and choose "Center Horizontally in Safe Area.”

That's it for Auto Layout, so please switch to the assistant view so we can add an action and an outlet. Ctrl-drag from the button to your code to create an outlet for it called tap. Then Ctrl-drag again to create an action for the button called tapped().

Every time the user taps the "Tap" button, we're going to execute a different animation. This will be accomplished by cycling through a counter, and moving an image view. To make all that work, you need to add two more properties to the class:

var imageView: UIImageView!
var currentAnimation = 0

There isn't an image view in the storyboard – we're going to create it ourself in viewDidLoad() using an initializer that takes a UIImage and makes the image view the correct size for the image.

Add this code to viewDidLoad():

imageView = UIImageView(image: UIImage(named: "penguin"))
imageView.center = CGPoint(x: 512, y: 384)
view.addSubview(imageView)

That places the penguin in the middle of an iPad-sized landscape screen, ready for us to animate.

There's one more thing we're going to do before we start looking at the animations, and that's to put a little bit of code into the tapped() method so that we cycle through animations each time the button is tapped. Put this in there:

currentAnimation += 1

if currentAnimation > 7 {
    currentAnimation = 0
}

That will add 1 to the value of currentAnimation until it reaches 7, at which point it will set it back to 0.

Upgrade your apps!

Take on Core ML, ARKit, PDFKit, Core NFC, and more all in one book that's packed with practical projects!

< Previous: Setting up   Next: Switch, case, animate: animate(withDuration:) >
MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>