2012-09-02 72 views
0

我想做一個UIButton的繼承。如果點擊會播放聲音,可以放大。當完成播放時,聲音回到原始狀態。 除了圖像沒有顯示,一切正常。 標題和背景會放大,聲音正在播放。 任何提示? soundButton.hUIButton繼承不顯示圖像

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
#import <AudioToolbox/AudioToolbox.h> 

@interface soundButton : UIButton <AVAudioRecorderDelegate, AVAudioPlayerDelegate>{ 
    BOOL    isPlaying; 
    AVAudioPlayer  *audioPlayer; 
} 
@property (nonatomic)BOOL  isPlaying; 
@property (nonatomic, strong) AVAudioPlayer *audioPlayer; 
- (id)initWithFrame:(CGRect)frame; 
-(void)loadSound:(NSString*)audiofile; 
-(void)touchUpInside; 
@end 

soundButton.m

#import "soundButton.h" 
@implementation soundButton 
@synthesize isPlaying; 
@synthesize audioPlayer; 
void audioRouteChangeListenerCallback (
            void *inUserData, 
            AudioSessionPropertyID inPropertyID, 
            UInt32 inPropertyValueSize, 
            id *inPropertyValue); 

-(BOOL)isPlaying{ 
return audioPlayer.isPlaying; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    [self addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside]; 
    UIImage *image = [UIImage imageNamed:@"kers.png"]; 
    [self setBackgroundImage:image forState:UIControlStateNormal]; 

} 
return self; 
} 
-(void)loadSound:(NSString*)audiofile{ 
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
            pathForResource:audiofile 
            ofType:nil]]; 

NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
if (error) 
{ 
    NSLog(@"Error in audioPlayer: %@", 
      [error localizedDescription]); 
} else { 
    audioPlayer.delegate = self; 
    [audioPlayer setVolume:1.0]; 
    [audioPlayer prepareToPlay]; 
    audioPlayer.currentTime = 0; 
} 
} 

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 
    [audioPlayer setCurrentTime:0]; 
self.transform = CGAffineTransformMakeScale(1.5,1.5); 
self.alpha = 1.0f; 
[UIView beginAnimations:@"fish" context:nil]; 
[UIView setAnimationDuration:1]; 
self.transform = CGAffineTransformMakeScale(1.0,1.0); 
self.alpha = 1.0f; 
[UIView commitAnimations]; 

self.isPlaying = NO; 
} 

-(void)touchUpInside{ 
//Check if something is playing If YES=>stop it 
if (self.isPlaying) { 
    [audioPlayer stop]; 
    [audioPlayer setCurrentTime:0]; 
} else { 

    self.transform = CGAffineTransformMakeScale(1.0,1.0); 
    self.alpha = 1.0f; 
    [UIView beginAnimations:@"fish" context:nil]; 
    [UIView setAnimationDuration:1]; 
    self.transform = CGAffineTransformMakeScale(1.5,1.5); 
    self.alpha = 1.0f; 
    [UIView commitAnimations]; 
} 


// Create an asynchronous background queue 
     NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
     [queue addOperationWithBlock: 
     ^{ 
       [audioPlayer play]; 
     }]; 
} 
@end 

在viewDidLoad中的實現的viewController:

UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"cherry.png"]; 
fishButton = [[soundButton alloc] initWithFrame:CGRectMake(323, 312, 123, 123)]; 

[fishButton setImage:image forState:UIControlStateNormal]; 
[fishButton loadSound:@"chime1.mp3"]; 
[fishButton setTitle:@"fish" forState:UIControlStateNormal]; 
[fishButton setBackgroundColor:[UIColor yellowColor]]; 
[fishButton setShowsTouchWhenHighlighted:YES]; 

[self.view addSubview:fishButton]; 

回答

1

替換此

UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"cherry.png"]; 

與此

UIImage *image = [UIImage imageNamed:@"cherry.png"]; 

,或者如果你真的需要使用initWithContentsOfFile使用URL鏈接像

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cherry" ofType:@"png"]; 
UIImage *image = [[UIImage alloc]initWithContentsOfFile:filePath]; 
+0

哇! Thanx!我剛剛發現添加: – YoYoHu

+0

+(id)buttonWithFrame:(CGRect)frame { return [[self alloc] initWithFrame:frame]; }也解決了這個問題......任何想法爲什麼? – YoYoHu

+0

不知道......你在之前的實現中遇到了問題,大概你已經修正了它而沒有注意到並且使用了另一個init調用,這讓你認爲這是解決問題的init調用。 – Eugene