0

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];
4

1 に答える 1

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];
于 2012-09-02T18:52:28.810 に答える