2012-02-29 97 views
0

我有一个用户移动一个对象,当移动结束时它将更新显示并播放声音。这一切都很好,但我不希望播放视图时播放声音。我如何避免这种情况发生?我需要在viewDidLoad中添加一些东西吗?这是触发事件的代码。谢谢你的帮助。 `想要保持播放视图时播放声音

// Updates the text field with the current rotation angle and play sound. 
- (void) updateTextDisplay 
{ 
NSError *error = nil; 
if (imageAngle >=0 && imageAngle <=180){ 
    ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"nice" ofType:@"m4a"]] error:&error]; 
    ComplimentPlayer.delegate = self; 
      textDisplay.text = [NSString stringWithFormat:@"blah blah blah"]; 
} else if (imageAngle >180 && imageAngle <=360){ 
    ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] 
     pathForResource:@"good" ofType:@"m4a"]] error:&error]; 
    ComplimentPlayer.delegate = self; 
      textDisplay.text = [NSString stringWithFormat:@"blah blah"]; 
} 
NSTimeInterval playbackDelay = 0.5; 
[ComplimentPlayer playAtTime: ComplimentPlayer.deviceCurrentTime + playbackDelay]; 

} `

回答

1

独立updateTextDisplay方法为两种方法

- (void) updateTextDisplay; 
- (void) playUpdateTextSound; 

别叫playUpdateTextSound()当视图第一次加载。 并且在调用updateTextDisplay()后调用playUpdateTextSound()

+0

谢谢,我会给你一个镜头。 – mmoore410 2012-02-29 23:37:49

1

编辑:难道整个视图重装或只是的UILabel?

设置一个int值,以检查视图第一次加载时的值,将值存储在用户默认值中,并在视图重新加载时更新值。然后在播放声音时在if逻辑中进一步比较。

在viewWillAppear中

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];//Add this or make it an ivar 
int a = 0; 
int b = ++a; //set int to add 1 to itself 
[defaults setInteger:b forKey:@"viewNumber"]; 
[[NSUserDefaults standardUserDefaults]synchronize]; 



    - (void) updateTextDisplay 
    { 
NSError *error = nil; 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; //Add this or make it an ivar 
if (imageAngle >=0 && imageAngle <=180 && [defaults [email protected]"viewNumber" >=2) //Add this here 
{ 
ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:   [[NSBundle mainBundle] pathForResource:@"nice" ofType:@"m4a"]] error:&error]; 
ComplimentPlayer.delegate = self; 
     textDisplay.text = [NSString stringWithFormat:@"blah blah blah"]; 
    } else if (imageAngle >180 && imageAngle <=360 && [defaults [email protected]"viewNumber">=2) //Add this here 
{ 
ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] 
    pathForResource:@"good" ofType:@"m4a"]] error:&error]; 
ComplimentPlayer.delegate = self; 
     textDisplay.text = [NSString stringWithFormat:@"blah blah"]; 
} 
NSTimeInterval playbackDelay = 0.5; 
    [ComplimentPlayer playAtTime: ComplimentPlayer.deviceCurrentTime + playbackDelay]; 

在AppDelegate中添加

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setInteger:0 forKey:@"viewNumber"]; //this restores the value to 0 upon termination 
[[NSUserDefaults standardUserDefaults]synchronize]; 
    } 
+0

当视图第一次加载位置时,默认为12:00位置,标签显示该位置的文本,我想我也会先隐藏,但声音文件也会播放,我不希望声音文件在用户第一次更改位置之前播放。对不起,我应该更具体。谢谢。 – mmoore410 2012-02-29 23:36:47