introduction to objective-c and xcode (part 5)
DESCRIPTION
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-audio-programmatically - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/1.jpg)
Introduction to Objective-Cand Xcode (Part 5)
FA 175Intro to Mobile App Development
![Page 2: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/2.jpg)
Agenda
• Sounds– System Sound Services
• Images– Image View and the UIImage class
![Page 3: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/3.jpg)
Sounds
• Reference:– http://www.raywenderlich.com/259/audio-101-fo
r-iphone-developers-playing-audio-programmatically
• 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 AudioToolbox library
![Page 4: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/4.jpg)
Setting up and playing sounds
• Set up– Define sound id
• SystemSoundId _soundId;
– Specify sound file• NSString *file = [[NSBundle mainBundle] pathForResource:
@"soundfile" ofType:@"wav"];• NSURL *url =[NSURL fileURLWithPath:file];
– Associate with sound id• AudioServicesCreateSystemSoundID((CFURLRef) url, &_soundId);
• Play– Play by indicating sound id
• AudioServicesPlaySystemSound(_soundId);
![Page 5: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/5.jpg)
Sounds class
• Create a Sounds class that “houses” sound-related functions
• Methods:– prepSounds: setup the different sounds to be used by
application (executes automatically through load method)– playSoundname: for each of the sound
• Make sure to– Include AudioToolbox library in frameworks– Include sound files in “Supporting Files”– Make the library and files part of project target
![Page 6: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/6.jpg)
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:– prepSounds: setup the sound ids (array)– setSound: associate file to an idNum (int)– play: plays the sound by indicating the idNum
![Page 7: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/7.jpg)
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 playWoosh]; // version 1[Sounds setSound: 3: @"file”: @"wav"]; // ver 2[Sounds play: 3]; // ver 2
![Page 8: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/8.jpg)
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 UIImageView outlet connection
![Page 9: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/9.jpg)
UIImage
• Set image of image view by assigning a UIImage object to the view’s image propertyself.imageView.image =
[UIImage imageNamed:@"x.jpg"];
• Make sure all image files needed are included in the “Supporting Files” area (and added to the project’s target
![Page 10: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/10.jpg)
Animation
• Set up an NSArray of images representing the images describing the animation
• Set two properties of the view– animationImages: the NSArray– animationDuration: number representing the
duration (in seconds) for one animation cycle• Call startAnimating method on the view
![Page 11: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/11.jpg)
Animation Example
NSArray *images = [NSArray arrayWithObjects: [UIImage imageNamed:@"x.jpeg"], [UIImage imageNamed:@"y.jpeg"], [UIImage imageNamed:@"z.jpeg"], nil]; self.iv.animationImages = images; self.iv.animationDuration = 0.5; [self.iv startAnimating];
![Page 12: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/12.jpg)
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
![Page 13: Introduction to Objective-C and Xcode (Part 5)](https://reader036.vdocuments.us/reader036/viewer/2022082505/56813240550346895d98b02c/html5/thumbnails/13.jpg)
Summary
• Sounds in an app can be played throughSystems Sound Services using the AudioToolbox 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– UIImageView class– UIImage class