Gone

This app launches in landscape orientation. I took the screenshot while the animation was in progress, so the letter G was caught between screen refreshes.

The app animates the center property of a UILabel which is a subview of another UIView. The label has white letters and a clear background; the view has a blue background and nothing else.

We put the audio code in the view controller because a subclass of UIView should concentrate on matters such as background color and fonts. A view should be seen and not heard. The audio code does not belong in the application delegate either. The application delegate should concentrate on lifecycle events—startup and termination.

We didn’t need to search for the image file in America using an NSBundle. But we do need to search for the sound file here using a NSBundle, because we need the full pathname of the sound file in order to create the NSURL object. Title sequence from the Gone With the Wind movie.

Source code in Gone.zip

  1. Class AppDelegate.swift
  2. Class ViewController.swift. Added the AVAudioPlayer property and initialized it in the viewDidLoad method. Also added the method supportedInterfaceOrientations to launch the app in lanscape orientation.
  3. Class View.swift
  4. gone.mp3
  5. Info.plist: added the “Initial interface orientation” property to launch the app in landscape orientation.

Create the project

Drag the file gone.mp3 into the Supporting Files folder of the Xcode Project Navigator.
Choose options for adding these files:
Destination: ☑ Copy items if needed.
Add to targets: ☑ Gone
Finish
Then open the Supporting Files folder and make sure gone.mp3 is listed there in black. Can you select it and play it?

To launch in landcape orientation, follow the “create the project” directions in Sine.

The application delegate has an instance variable that is an AVAudioPlayer. The file ViewController.swift must therefore import the module AVFoundation. Surprisingly, we do not have to add the AVFoundation.framework to the project.

Run the project

Turn on the Macintosh sound. Run the project. Does the sound stop when you press the simulator’s Home button?
Hardware → Home

Output produced by print on the iPhone Simulator:

bundle.bundlePath = "/Users/myname/Library/Developer/CoreSimulator/Devices/01EBED35-F21B-4B4C-95BC-BFDDDC6264B4/data/Containers/Bundle/Application/DADF37D1-AB36-4C40-B31B-4FD8B023584B/Gone.app"

path = "/Users/myname/Library/Developer/CoreSimulator/Devices/01EBED35-F21B-4B4C-95BC-BFDDDC6264B4/data/Containers/Bundle/Application/DADF37D1-AB36-4C40-B31B-4FD8B023584B/Gone.app/gone.mp3"

url = "file:///Users/myname/Library/Developer/CoreSimulator/Devices/01EBED35-F21B-4B4C-95BC-BFDDDC6264B4/data/Containers/Bundle/Application/DADF37D1-AB36-4C40-B31B-4FD8B023584B/Gone.app/gone.mp3"

player!.numberOfChannels = 2

Things to try

  1. Fade in a pair of UILabels (or a single UITextView; see here) containing the following two lines. Animate the alpha of the UILabels from 0.0 to 1.0.

    David O. Selznick
    Presents

  2. Animate the STAR WARS logo receding from the viewer, with background music.


  3. It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire. During the battle, Rebel spies managed to steal secret plans to the Empire’s ultimate weapon, the Death Star, an armored space station with enough power to destroy an entire planet. Pursued by the Empire’s sinister agents, Princess Leia races home aboard her starship, custodian of the stolen plans that can save her people and restore freedom to the galaxy… See crawl.html here.