pickview加载图片和声音

该博客展示了如何在iOS应用中使用PickerView加载图片并配合AudioToolbox播放声音。通过实现PickerView的数据源和代理方法,创建了一个能够显示不同图片的Picker,并在特定条件下播放预设的.wav音频文件。
摘要由CSDN通过智能技术生成

 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值