我想创建一个播放声音的按钮,并在完成声音后更改按钮的图像。我很难在声音和图像变化之间进行同步。
我有一个控制器类,按下按钮后会调用一个声音类。声音类播放音频文件(使用 AVAudioPlayer),完成后调用委托 audioPlayerDidFinishPlaying。此委托应该更改图像,但声音委托位于声音类中,而按钮的属性位于控制器类中,因此委托无法与按钮通信。
(我想过向声音类发送一个指向按钮的指针,或者我可以使用 NSNotificationCenter,但我真的不确定)
问题是委托如何导致 UIButton 的图像变化?
这是一些代码:
// try1ViewController.h
@class AVSnd;
@interface try1ViewController : UIViewController <AVAudioPlayerDelegate>{
AVSnd *mAvs1;
}
@property (nonatomic, retain) AVSnd *mAvs1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@end
// try1ViewController.m
- (IBAction)Button2Pressed:(id)sender
{
//...
self.mAvs1 = [[AVSnd alloc] initWithCafFile:[NSArray arrayWithObjects: audioX2, audioY1, nil ]];
//...
[myButton2 setImage:[UIImage imageNamed:image1] forState:UIControlStateNormal];
//...
}
// AVSnd.h
@interface AVSnd : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer *mSoundPlayer;
NSArray* fileNameQueue;
int index;
}
@property (nonatomic, retain) AVAudioPlayer *mSoundPlayer;
@property (nonatomic, retain) NSArray* fileNameQueue;
@property (nonatomic, assign) int index;
-(id)initWithCafFile: (NSArray*)queue;
-(void)playWithNum: (int) num;
-(void)fadeOut;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)mSoundPlayer successfully:(BOOL)flag;