Introduction to ObjectiveC and Xcode Part 5 FA
Introduction to Objective-C and Xcode (Part 5) FA 175 Intro to Mobile App Development
Agenda • Sounds – System Sound Services • Images – Image View and the UIImage class
Sounds • Reference: – http: //www. raywenderlich. com/259/audio-101 for-iphone-developers-playing-audioprogrammatically • Various ways to play sound, depending on format, length, and whether you want sounds played in the background • For this class, we will use System Sound Services via the Audio. Toolbox library
Setting up and playing sounds • Set up – Define sound id • System. Sound. Id _sound. Id; – Specify sound file • NSString *file = [[NSBundle main. Bundle] path. For. Resource: @"soundfile" of. Type: @"wav"]; • NSURL *url =[NSURL file. URLWith. Path: file]; – Associate with sound id • Audio. Services. Create. System. Sound. ID((CFURLRef) url, &_sound. Id); • Play – Play by indicating sound id • Audio. Services. Play. System. Sound(_sound. Id);
Sounds class • Create a Sounds class that “houses” soundrelated functions • Methods: – prep. Sounds: setup the different sounds to be used by application (executes automatically through load method) – play. Soundname: for each of the sound • Make sure to – Include Audio. Toolbox library in frameworks – Include sound files in “Supporting Files” – Make the library and files part of project target
Sounds class (version 2) • More flexible: allows adding of sound files from outside of the Sounds class – For the first version, you would need to update the Sounds class for every application • Methods: – prep. Sounds: setup the sound ids (array) – set. Sound: associate file to an id. Num (int) – play: plays the sound by indicating the id. Num
Class methods revisited • Take note: the methods of the Sounds class are class methods • . h file (interface) contains method declarations with the + prefix • When using the class, [Sounds <method>]; – e. g. , [Sounds play. Woosh]; // version 1 [Sounds set. Sound: 3: @"file”: @"wav"]; // ver 2 [Sounds play: 3]; // ver 2
Images • Image View – Displays an image or animation (described by several images) – Placed within a UI View of a View Controller • Associated image can be assigned either – Through the Xcode environment: through attributes inspector – Or programmatically, by setting properties or calling methods on the UIImage. View outlet connection
UIImage • Set image of image view by assigning a UIImage object to the view’s image property self. image. View. image = [UIImage image. Named: @"x. jpg"]; • Make sure all image files needed are included in the “Supporting Files” area (and added to the project’s target
Animation • Set up an NSArray of images representing the images describing the animation • Set two properties of the view – animation. Images: the NSArray – animation. Duration: number representing the duration (in seconds) for one animation cycle • Call start. Animating method on the view
Animation Example NSArray *images = [NSArray array. With. Objects: [UIImage image. Named: @"x. jpeg"], [UIImage image. Named: @"y. jpeg"], [UIImage image. Named: @"z. jpeg"], nil]; self. iv. animation. Images = images; self. iv. animation. Duration = 0. 5; [self. iv start. Animating];
Exercise • Create a single view project with three buttons and one image view – Set the image of the view to some default image • Button 1: changes the image the another image • Button 2: plays a sound • Button 3: plays a different sound and then runs an animation • The project requires 2 sounds and at least 4 images
Summary • Sounds in an app can be played through Systems Sound Services using the Audio. Toolbox library – Other frameworks are available – A Sounds class supporting sound support has been created for this course to simplify use • Images and animation supported through – UIImage. View class – UIImage class
- Slides: 13