pickview加载图片和声音
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
//#import <AudioToolbox/AudioServices.h>
@interfaceViewController ()
@end
@implementation ViewController
@synthesize column1;
@synthesize column2;
@synthesize column3;
@synthesize column4;
@synthesize label;
@synthesize picker;
@synthesize selectButton;
-(void)spin
{
BOOL win=NO;
int numInRow=1;
int lastVal=-1;
for (int i=0; i<5; i++) {
int newValue=random()%[self.column1count];
if (newValue==lastVal) {
numInRow++;
}
else
{
numInRow=1;
}
lastVal=newValue;
[picker selectRow:newValueinComponent:i animated:YES];
[pickerreloadComponent:i];
if (numInRow>=3) {
win=YES;
}
}
selectButton.hidden=YES;
NSString *path=[[NSBundlemainBundle]pathForResource:@"crunch"ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridgeCFURLRef)[NSURLfileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
if (win) {
ClickIndex++;
[selfperformSelector:@selector(playWinSound)withObject:nilafterDelay:.5];
}
else
{
ClickIndex++;
[selfperformSelector:@selector(showButton)withObject:nilafterDelay:.5];
label.text=[[NSStringalloc]initWithFormat: @"第%d次",ClickIndex ];
}
}
-(void) showButton
{
selectButton.hidden=NO;
}
-(void) playWinSound
{
NSString *path=[[NSBundlemainBundle]pathForResource:@"win"ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridgeCFURLRef)[NSURLfileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
label.text=[[NSStringalloc ]initWithFormat: @"You WIN,play times is %d",ClickIndex ];
[selfperformSelector:@selector(showButton)withObject:nilafterDelay:1.5];
ClickIndex=0;
}
- (void)viewDidLoad
{
self.picker.delegate=self;
self.picker.dataSource=self;
ClickIndex=0;
UIImage *seven=[UIImage imageNamed:@"seven.png"];
UIImage *bar=[UIImage imageNamed:@"bar.png"];
UIImage *crown=[UIImage imageNamed:@"crown.png"];
UIImage *lemon=[UIImage imageNamed:@"lemon.png"];
UIImage *apple=[UIImage imageNamed:@"apple.png"];
UIImage *cherry=[UIImage imageNamed:@"cherry.png"];
for (int i=1; i<=5; i++) {
UIImageView *sevenView=[[UIImageView alloc]initWithImage:seven];
UIImageView *barView=[[UIImageView alloc]initWithImage:bar];
UIImageView *crownView=[[UIImageView alloc]initWithImage:crown];
UIImageView *lemonView=[[UIImageView alloc]initWithImage:lemon];
UIImageView *appleView=[[UIImageView alloc]initWithImage:apple];
UIImageView *cherryView=[[UIImageView alloc] initWithImage:cherry];
NSArray *imageViewArray=[[NSArray alloc]initWithObjects:sevenView,
barView,crownView,lemonView,appleView,cherryView,nil];
NSString *fieldName=
[[NSStringalloc] initWithFormat:@"column%d",i];
[self setValue:imageViewArrayforKey:fieldName];
//[fieldName release];
}
srandom(time(NULL));
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -
#pragma Picker Data Source Methods
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return5;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.column1count];
}
#pragma mark Picker Delegate Methods
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSString *arrayName=[[NSString alloc]initWithFormat:@"column%d",component+1];
NSArray *array=[self valueForKey:arrayName];
return [array objectAtIndex:row];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end