2013-10-18 100 views
1

我是Objective-C的新手,並且編碼全部。我已經開始創建一個應用程序,將圖片從屏幕上部拉到屏幕底部,並在底部釋放聲音。當imageView被移動時觸發聲音

#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座標來做聲音效果並以這種方式觸發聲音。但我現在不知道如何編碼。

回答

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); 
    } 
} 
+0

我不明白如何使用此代碼。如上所述,這裏總是新手。 –

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]; 
    } 
} 
1

佳兆業科伊維斯托,Indra的點正確播放聲音。我試試這工作正常。

相關問題