0

As the title describes I've been able to add sound when button is pressed, but I was wondering if there was a way to also add an image change to the main UIImageView when the same button is pressed. Is there any way to stick the picture code in where the "playFaceOfAudioType" method is presented?

Here's my code so far:

#import "FourthViewController.h"

@interface FourthViewController ()

@end

@implementation FourthViewController

@synthesize audioPlayer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)facesAudioAction:(id)sender {
    UIButton *btn=(UIButton *)sender;
    [self playFaceAudioOfType:btn.tag];//Tag for button is set on the xib..

}




-(void)playFaceAudioOfType:(int)type{

    [self stopAudio];


    NSString *sound=@"";

    switch (type) {
        case 1:
            sound=@"1";
            break;

        case 2:
            sound=@"2";
            break;

        case 3:
            sound=@"3";
            break;

        case 4:
            sound=@"4";
            break;

        case 5:
            sound=@"5";
            break;

        default:
            break;
    }


    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:sound
                                         ofType:@"mp3"]];



    NSError *error;
    if(url){
        audioPlayer = [[AVAudioPlayer alloc]
                       initWithContentsOfURL:url
                       error:&error];



        if (error)
        {
            NSLog(@"Error in audioPlayer: %@",
                  [error localizedDescription]);
        } else {
            audioPlayer.delegate = self;
            [audioPlayer prepareToPlay];
        }

        [audioPlayer play];
    }


}




-(void)stopAudio{

    if(audioPlayer && [audioPlayer isPlaying]){
        [audioPlayer stop];
        audioPlayer=nil;
    }


}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I understand from looking around that usually this is done by doing an if / else if type of approach but was wondering if it was possible to include it along with the switch statement bit.

4

2 回答 2

1

您可以使用其中一种facesAudioActionplayFaceOfAudioType方法来更改图像。

此外,您可以使用以下命令摆脱 switch 语句;

NSString *sound = [NSString stringWithFormat:@"%d", type];

您可以像这样设置图像;

NSString *imageName = [NSString stringWithFormat:@"%d.jpg", type];
yourImageView.image = [UIImage imageNamed:imageName];
于 2013-07-22T07:16:26.890 回答
-1

如果你使用 UIControl 的标签,你定义NSString *sound = [NSString stringWithFormat:@"%d",type];在 playFaceAudioOfType:function.then 你可以避免 switch 语句。

而解决方案2,您可以使用setImage:forState:方法显示按钮的样式,并使用button.titleLabel.text保存它的歌曲信息(例如名称)。

于 2013-07-22T09:02:17.273 回答