0

プログラムのラベルを更新するタイマーがあり、指を下に向けたりスクロールしたりするなどのユーザー操作が発生すると、タイマーは一時停止します。私はこのコードを使用して修正していますが、メモリ割り当てを再作成しているため、ゲームが数分以内にクラッシュするようです。

-(void) timeChangerInitNormal {
    if (running == YES)
        if ( _timerQueue ) return;
    _timerQueue = dispatch_queue_create("timer queue", 0);

    dispatch_async(_timerQueue, ^{

        timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timeChanger) userInfo:nil repeats:YES];

        while ( [timer isValid] ) {
            [[NSRunLoop currentRunLoop] runUntilDate:[[NSDate date] dateByAddingTimeInterval:5.0]];
        }
    });
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
{
    [self _lockInteraction];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
    [self _unlockInteraction];
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    [self _lockInteraction];    
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;
{
    [self _unlockInteraction];
}

- (void) _lockInteraction
{
    [self _setControls:_staticViews interacted:NO];
    [self _setControls:_autoLayoutViews interacted:NO];
}

- (void) _unlockInteraction
{
    [self _setControls:_staticViews interacted:YES];
    [self _setControls:_autoLayoutViews interacted:YES];
}

- (void) _setControls:(NSArray *)controls interacted:(BOOL)interacted
{
    for ( UIControl *control in controls ) {
        if ( [control isKindOfClass:[UIControl class]]) {
            control.userInteractionEnabled = interacted;
        }
    }
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    [self _updatePositionForViews:_autoLayoutViews];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self _updatePositionForViews:_autoLayoutViews];
}

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView;
{
    return _mapImageView;
}
4

1 に答える 1

1

tl; dr

通常のタイマーを使用したいだけです。作成したら、イベント追跡モードに追加します。

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];

これにより、イベントの追跡中にトリガーされます(スライダーのタッチダウン、スクロールビューなど)。

NSDateそれでも知りたい場合:基本的に、whileループでCPUをビジー状態に保ち、自動解放されたオブジェクトを作成するスレッドを作成しました。内部自動解放プールがなかったため、日付オブジェクトは解放されませんでした。

ただし、古いコードを修正しようとしないでください。さらに問題があります。

于 2012-11-27T19:43:58.330 に答える