-1

を使用してループプロセスを使用する方法を教えてくれる人はいますNSTimerか? ボタンによってトリガーされる 2 つのプロセス タイマーがあり、timer1 と timer2 が n ループ (timer1 -> timer2 -> timer1 -> timer2 など、n ループまで繰り返される) に繰り返されます。私はxcodeが初めてです。教えて下さい。両方のタイマーにユーザーによる入力があります。可能であれば例を教えてください。

私のコードは次のようになります。私が間違っていたら訂正してください

- (void) timer2Elapsed:(NSTimer *) timer;
{
    ...

    displayCountDown.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}

- (void)timer1Elapsed: (NSTimer *) timer;
{
    ...

    displayCountDown.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}

カウントダウンをトリガーする私のボタン:

- (IBAction)startBtn:(id)sender {
        endSetTime = [NSDate timeIntervalSinceReferenceDate] + totalSecondTime;
        endSetRest = [NSDate timeIntervalSinceReferenceDate] + totalSecondRest;
        countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 0.99 target: self selector: @selector(timer1Elapsed:) userInfo: nil repeats: YES];

}

ここの誰かがこのコードを使用するように私に言いましたが、中に何を書き、どこに置くべきかわかりませんか? 彼はループのために言った?知らない?そして、ボタン内でそのコードを接続する方法は?

+ (void) startTimer:(id)timer
{
    static int numberOfTimes = 0;
    if(numberOfTimes >= 5)
    {
        numberOfTimes = 0;
        return;
    }
    numberOfTimes ++;

    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer1Elapsed:) userInfo:nil repeats:NO];
}
4

1 に答える 1

0

タイマーを忘れてください。performSelector:withObject:afterDelay: セレクターを使用します。

@interface CECountdown : NSObject {
    NSDate *_startDate;
    NSTimeInterval _countdownInterval;
}

@end

@implementation CECountdown

- (id)initWithCountdownInterval:(NSTimeInterval)interval {
    self = [super init];

    if(self) {
        _countdownInterval = interval;
        _startDate = [[NSDate date] retain];
    }

    return self;
}

- (void)dealloc {
    [_startDate release];

    [super dealloc];
}

- (void)method0 {
    // Do something
    [self performSelector:@selector(method1) withObject:nil afterDelay:0.3];
    [self checkTimeout];
}

- (void)method1 {
    // Do something
    [self performSelector:@selector(method0) withObject:nil afterDelay:0.3];    
    [self checkTimeout];
}

- (void)invalidate {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method1) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method2) object:nil];
}

- (void)checkTimeout {
    if([NSDate timeIntervalSinceReferenceDate] - [_startDate timeIntervalSinceReferenceDate] > _countdownInterval) {
        [self invalidate];
    }
}

- (void)didTapStartButton:(id)sender {
    [self performSelector:@selector(method1) withObject:nil afterDelay:0.3];
}

@end
于 2012-06-27T08:36:10.500 に答える