iphone - 如何在 iOS 5 中运行多个循环和条件检查?

标签 iphone ios xcode ipad loops

我有一个适用于 iPad iOS 5 的程序,它读取 MIDI,然后在键盘上与音乐同步显示音符。它工作正常,但我试图添加一个“重复部分”功能,它会一遍又一遍地重复从时间戳 A 到时间戳 B 的部分。

我已经能够让时间戳作为我的重复部分的边界工作,但我无法让重复正常工作。当我尝试重复一个部分时,我再也看不到键盘动画了。我觉得这个问题需要超线程,但我不确定。我在下面用伪代码概述了我想做的事情。

//Start Repeat Method
while (the repeat switch is toggled) {
     Stop music player.

     Set music player to the start point of the repeat.

     while (the current play point is before the end point of the repeat) {
          Check the current play point.
     }
}
//End Repeat Method

基本上,我想做的是在用户按下开关时触发一个方法,该方法将被反复调用,直到他们将其关闭。在该方法中,它将停止播放器,将其设置为重复的开始,播放重复直到它看到它在重复的结尾,然后重新开始该方法。

我认为这部分不会像以前那样棘手。我遇到的另一个问题是,当我将它连接到一个开关时,它不允许我将其关闭,它只会永远持续下去。

提前感谢您的建议。

**编辑

这是我目前所拥有的。它允许我循环我的部分,但我的动画正在显示,我无法与 UI 交互,我必须使用 Xcode 中的停止按钮终止程序。

- (IBAction)playRepeat:(id)sender {
     if (repeatToggle.on) {
          MusicPlayerStop(player);
          playerIsPlaying = NO;

          MusicPlayerSetTime(player, sequenceRepeatStartTime);
          moviePlayerViewController.moviePlayer.currentPlaybackTime = rollRepeatStartTime;

          MusicPlayerStart(player);
          [moviePlayerViewController.moviePlayer play];
          playerIsPlaying = YES;

          float difference = rollRepeatEndTime - rollRepeatStartTime;
          [NSThread sleepForTimeInterval:difference];

          MusicPlayerStop(player);
          playerIsPlaying = NO;
          [moviePlayerViewController.moviePlayer pause];

          [self playRepeat:sender];
     }
     else if (!repeatToggle.on) {
          MusicPlayerStop(player);
          playerIsPlaying = NO;
     }
}

最佳答案

您的 while 循环会耗尽 CPU,因为它一直在运行并且什么都不等待。 将它放在单独的线程中可能会有所帮助,但如果您的播放器不是线程安全的,您将需要锁定机制。

并不是说如果没有播放器本身的通知,很难在正确的时间重复它。您应该检查您的 MIDI 播放器是否支持任何通知或委托(delegate)回调,您可以使用它们在播放达到您指定的点时收到通知。

无论如何,我会提供可能适合您的出路。您可以使用计时器检查播放器,也许每 100 毫秒执行一次这样的操作。

-(void) repeatCheck {
    if (the repeat switch is ON) {
        if (the current play point is NOT before the end point of the repeat) {
            Stop music player.
            Set music player to the start point of the repeat.
        }
    }
    [self performSelector:_cmd withObject:nil afterDelay:0.1];
}

-(IBAction) repeatSwitchToggled {
    if (the repeat switch is ON) {
        [self repeatCheck];
    }
    else {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(repeatCheck) object:nil];
    }
}

关于iphone - 如何在 iOS 5 中运行多个循环和条件检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10859299/

相关文章:

iphone - CGAffineTransform Scale 在设备旋转后修改

iphone - Objective-C,使用 UI 事件取消调度队列

ios - 缩放 UIWebView 内容

ios - UITableView - 滚动到顶部

ios - 使用 Swift 的 Core Graphics/Quartz Core resizing 从较小尺寸调整到较大尺寸

ios - iOS 中的设备兼容性问题

iphone - 如何使用查找和替换在 Xcode 中用单个新行替换多个新行

iphone - 一个应用程序中可以有AVFramework和AudioToolbox框架吗?

ios - 如何让一个操作等待另一个操作快速完成

ios - 为方便起见,不能使用#define