This was my example. When you clicked the button, the face appeared and a sound file of a scream played.
I also suggested, to help students understand the interaction, that they could start with a picture of an animal, like a pig. If you clicked the tail, the image should change and a squeal should play.
iPhone Project with buttons and sound
- Create a background image
- Create a foreground image
- Create an icon image 57 x 57px
- Find a sound. Open the sound in Audacity and save as 32-bit sound.
- Open XCode
- Create a ViewBased Project
- Add the images and the sound
- Update the plist file
- Add a framework by CTRL+clicking on Frameworks. Select Existing Frameworks and add AudioToolbox.framework
- Open the header file
- Create a statement that imports the AudioToolbox/AudioToolbox.h
- Add 3 IBOutlets. One to hold a UILabel and the other two to hold pointers of type UIImageView
- Create a pointer of type NSTimer and name it resetTimer
- Create a a variable named sound of type SystemSoundID
- Create 3 @property declarations for the label and the UIImageViews
- Create a buttonPressed IBAction that takes sender of type id as its parameter.
- Open the implementation file
- @synthesize the properties
- Define the buttonPressed method:
- Set the text of your label to your message
- Create a pointer named foregroundImg of type UIImage and set it to the UIImage method imageNamed with the name of your foreground image as your parameter
- Set the image of the UIImageView that will pop up, to be the variable you just created (foregroundImg)
- Set the hidden property of that UIImageView to NO
- Test if sound and
if the condition is met, then call:
AudioServicesPlaySystemSound(sound);
- Test if resetTimer does NOT equal nil
If the condition is met, then call:
[resetTimer invalidate];
- Now set resetTimer to
resetTimer = [NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector(resetImages:) userInfo:nil repeats:NO];
- Now you have to define the method you just called (resetImages)
- (void)resetImages:(NSTimer *)sender{}
- Inside the method:
- Set the hidden property of the UIImageView that pops up to YES
- Empty the label.
- Set resetTimer to nil
- Navigate to and uncomment viewDidLoad
- After the call to super, include these lines (remember to update the name of your audio file):
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundUrlRef = CFBundleCopyResourceURL(mainBundle, CFSTR ("aaaah2"), CFSTR ("wav"), NULL);// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundUrlRef, &sound);
- Open the nib file and add a UIImageView
- Using the inspector set it to hold your background image
- Add a label
- Add a button
- Add another UIImageView and align it with the bottom
- Connect The File Owner to label and select label
- Connect the button to the File Owner and select buttonPressed
- Connect the File Owner to the UIImageView aligned to the bottom and select the name holding the UIImageView that pops up
This project was inspired by Rory Lewis’s tutorials



