1

我是 Objective-C 和编码的新手。我已经开始创建一个应用程序,将图片从屏幕上部拉到屏幕底部,它应该在底部释放声音。

#import "TestiAppViewController.h"


@interface TestiAppViewController ()

@end

@implementation TestiAppViewController

-(IBAction)controlPan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x, recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0,0) inView:self.view];
}


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

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

@end

如果我理解正确,我应该能够通过使用 y 坐标并以这种方式触发声音来制作音效。但我现在不知道如何编码。

4

3 回答 3

1
- (void)playAudio {
    [self playSound:@"soundFile" :@"wav"];
}

- (void)playSound :(NSString *)fName :(NSString *) ext{
    SystemSoundID audioEffect;
    NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
    if ([[NSFileManager defaultManager] fileExistsAtPath : path]) {
        NSURL *pathURL = [NSURL fileURLWithPath: path];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
        AudioServicesPlaySystemSound(audioEffect);
    }
    else {
        NSLog(@"error, file not found: %@", path);
    }
}
于 2013-10-18T06:32:30.500 回答
1

Kaisa Koivisto,Indra 对播放声音的观点是正确的。我试过了,效果很好。

于 2013-10-24T11:49:57.513 回答
0

你可以试试下面的代码

-(IBAction)controlPan:(UIPanGestureRecognizer *)recognizer
{
  CGPoint translation = [recognizer translationInView:self.view];
  recognizer.view.center = CGPointMake(recognizer.view.center.x, recognizer.view.center.y + translation.y);
  [recognizer setTranslation:CGPointMake(0,0) inView:self.view];
  if(translation.y + recognizer.frame.size.height == self.view.frame.size.height)
   //now the image is in bottom of the view.
  {
     [self playaudio];
  }
}
于 2013-10-18T06:36:44.537 回答