我正在尝试继承UIButton。如果点击将发出声音,放大。当完成播放声音音阶回到原始状态。 一切正常,但图像没有显示。 标题和背景确实放大,声音正在播放。 有人提示吗? 的 soundButton.h
#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
在viewController中的ViewdidLoad中实现:
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];
答案 0 :(得分:1)
替换此
UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"cherry.png"];
用这个
UIImage *image = [UIImage imageNamed:@"cherry.png"];
或者如果您确实需要使用initWithContentsOfFile
,请使用
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cherry" ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:filePath];