0

IBActionのを調整するノブがtimeIntervalありNSTimerます。

しかし、調整中にタイマーを継続的に起動させる方法が見つかりませんtimeInterval。これは、タイマーを継続的に無効化してインスタンス化しているためだと思いますよね?

タイマーがノブの動きで加速/減速するように、スムーズに動作させる方法はありますか?

-(IBAction)autoSpeed:(UISlider *)sender
{
    timeInterval = (60/sender.value) / 4;

    if (seqState){
        [self changeTempo];
    }

    [self displayBPM:[sender value]:[sender isTouchInside]];
}

-(void) changeTempo
{
    if (repeatingTimer!= nil) {
        [repeatingTimer invalidate];
        repeatingTimer = nil;
        repeatingTimer = [NSTimer scheduledTimerWithTimeInterval: timeInterval target:self selector:@selector(changeAutoSpeedLed) userInfo:nil repeats:YES];

    }
    else
        repeatingTimer = [NSTimer scheduledTimerWithTimeInterval: timeInterval target:self selector:@selector(changeAutoSpeedLed) userInfo:nil repeats:YES];
}
4

2 に答える 2

1

ティックでタイマーを再作成できます。


.hファイル

間隔と呼ばれるプロパティを作成する必要があります。

@property NSTimeInterval interval; 

.mファイル

まず、初期化します。

self.interval = 100;
[self timerTick];

次に、このtimerTickメソッドを使用してタイマーを再作成できます。

- (void)timerTick {
    if (self.interval) {
        [self.timer invalidate];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerTick) userInfo:nil repeats:YES];
        self.interval = 0;
    }


    // Do all the other stuff in the timer
}

その後、self.intervalいつでも設定でき、タイマーが自動的に再作成されます。

于 2012-12-26T15:18:57.557 に答える
1

スムーズに実行されていない理由は、scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:Apple のドキュメントによると、を使用しているためです。

新しい NSTimer オブジェクトを作成して返し、デフォルト モードで現在の実行ループにスケジュールします。

デフォルト モードは UI インタラクションによってブロックされるため、ノブを制御している場合、タイマーはブロックされます。代わりに次のようなコードを使用する場合:

[[NSRunLoop currentRunLoop] addTimer:repeatingTimer forMode:NSRunLoopCommonModes];

コードは UI によってブロックされません。

于 2012-12-26T15:49:23.493 に答える