移动imageView时触发声音

时间:2013-10-18 06:25:29

标签: ios objective-c uipangesturerecognizer

我是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坐标和触发声音来完成声音效果。但我现在不知道如何编码。

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:1)

Kaisa Koivisto,英德拉对于播放声音的观点是正确的。我试过这个工作正常。

答案 2 :(得分: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];
  }
}