objective-c - iOS:以逐渐增加的速度触发方法

标签 objective-c ios cocoa-touch gamekit

我的游戏每秒播放滴答声。我希望声音在整个游戏过程中慢慢加快。我最初的想法是使用 NSTimer 并在方法触发时更新速度,如下所示:

static float soundDelay = 1.0;
timer = [NSTimer scheduledTimerWithTimeInterval:clickClackDelay
                                                     target:self
                                                   selector:@selector(playSound)
                                                   userInfo:nil
                                                    repeats:YES];

- (void)playSound {
    soundDelay -= 0.1;
    NSLog(@"Play sound");
}

这没有用,而且看起来 NSTimer 并不是真的应该以这种方式使用。关于如何实现此目标的任何其他建议?

最佳答案

您可以通过从自身调用playSound 方法来实现它。 您可以通过以下方式进行。

- (void)playSound
{
   static float soundDelay = 1.0;
   if([timer isValid])
   {
     [timer invalidate];
     timer = nil;
   }
   timer = [NSTimer scheduledTimerWithTimeInterval:clickClackDelay
                                                     target:self
                                                   selector:@selector(playSound)
                                                   userInfo:nil
                                                    repeats:NO];
    soundDelay -= 0.1;
    if(soundDelay <=0)   //when sound delay is zero invalidate timer
    {
       [timer invalidate];
       timer = nil;
    }
    NSLog(@"Play sound");
}

关于objective-c - iOS:以逐渐增加的速度触发方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064220/

相关文章:

ios - VPN - NEPacketTunnelProvider - 后台模式

ios - 使用 NSAttributedString 更改字符串颜色?

objective-c - 一旦由 UILongPressGestureRecognizer 触发拖动它时,如何使我的 UIView 保持在 "jumping"?

iphone - document目录在objective c中存放数据好还是缓存好?

ios - swift iOS : UIPageController inside UINavigation controller

iphone - 使用 setFrame 更改位置后 UIButton 不响应触摸事件

ios - Swift 中 '_ in print()' 是什么意思?

ios - UIActivityIndi​​catorView 不显示

ios - 没有自动布局的 iphone 6 加载 Storyboard 的条件

ios - Eureka 表格 : How to change form global tint colour