2012-03-09 3 views
3

Я не буду использовать в классе этот метод, чтобы начать действие:cancelPreviousPerformRequestsWithTarget не работает

[self performSelector:@selector(startRolling) withObject:nil afterDelay:0.1f]; 

Для того, чтобы остановить его я использую:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil]; 

Но никогда останавливается.

Оба вызова реализованы в одном классе и в том же потоке.

Я использовал это тоже, но это не решает его:

-(void)stopRolling 
{ 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil]; 

    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self]; 
} 

Что я пропускаю?

Спасибо.

--- EDIT ---

Чтобы узнать, если Главный поток Я использую:

-(void)stopRolling 
{ 
    if ([NSThread isMainThread]) 
     NSLog(@"Is Main Thread"); 

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil]; 

    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self]; 
} 

Когда работает нормально, и когда не всегда появляется Бревно Главная тема.

Что делают селекторы я запуская (startRolling и commitAnimations) является анимация с использованием [UIView beginAnimation: контекст :)

Возможно, что это и есть причина?

Здесь методы:

-(void)startRolling 
{ 
    currentPic++; 

    if (currentPic > count) 
     currentPic = 1; 

    [UIView beginAnimations:@"fadeIn" context:nil]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:RDFadeInDelay]; 

    NSLog(@"RollUp: %@",[NSString stringWithFormat:@"RDInitialRollUp_%d",currentPic]); 

    background.image = [UIImage imageNamed:[NSString stringWithFormat:@"RDInitialRollUp_%d.jpg",currentPic]]; 

    background.alpha = 1.0f;  

    [UIView commitAnimations]; 

} 
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *) finished context:(void *) context 
{ 
    if ([animationID isEqualToString:@"fadeIn"]) 
    { 
     [self performSelector:@selector(commitAnimation) withObject:nil afterDelay:RDOnScreenDelay]; 
    } 
    else 
    { 
     [self performSelector:@selector(startRolling) withObject:nil afterDelay:0.1f]; 

    } 
} 
-(void)commitAnimation 
{ 
    [UIView beginAnimations:@"fadeOut" context:nil]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:RDFadeOutDelay]; 

    background.alpha = 0.0f;  

    [UIView commitAnimations]; 
} 
-(void)stopRolling 
{ 
    if ([NSThread isMainThread]) 
     NSLog(@"Is Main Thread"); 

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil]; 

    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self]; 
} 

Метод, который вызывает метод stopRolling вызывается с помощью performOnMainThread:

Благодарности.

--edit ---

Я изменил метод с предложениями, но по-прежнему не работает:

-(void)stopRolling 
{ 
    if ([NSThread isMainThread]) 
     NSLog(@"Is Main Thread"); 
    else 
     NSLog(@"Is NOT Main Thread"); 

    [self.layer removeAllAnimations]; 

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil]; 

    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self]; 
} 

Я заметил, что если пользователь вводит экран при переходе между изображения выполняются, когда не работает. Но если пользователь нажимает на экран, когда изображение находится на экране (в течение 2 секунд), все работает нормально.

Всегда на главной теме.

:(Я desesperated, это делает сбой программы, так как память.

--EDIT--

Наконец я решил, используя флаг BOOL. Но я думаю, что это плохое решение, потому что Это должно работает без Что-то странное происходит в то время как анимация выполняется, потому что это только работает, когда анимация не выполняется

Это работает для всех случаев:..

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *) finished context:(void *) context 
{ 
    if (!mustAnimate) return; 

    if ([animationID isEqualToString:@"fadeIn"]) 
    { 
     [self performSelector:@selector(commitAnimation) withObject:nil afterDelay:RDOnScreenDelay]; 
    } 
    else 
    { 
     [self performSelector:@selector(startRolling) withObject:nil afterDelay:0.1f]; 

    } 
} 
-(void)commitAnimation 
{ 
    [UIView beginAnimations:@"fadeOut" context:nil]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:RDFadeOutDelay]; 

    background.alpha = 0.0f;  

    [UIView commitAnimations]; 
} 
-(void)stopRolling 
{ 
    mustAnimate = NO; 

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil]; 

} 

Спасибо за все.

+1

Вы уверены, что вы проверили нить? Он должен работать ... Как насчет того, чтобы установить большую задержку, чтобы увидеть, работает ли он? – fbernardo

+0

В нескольких тестах я заметил, что иногда останавливается, а иногда нет. Но самым любопытным является то, что когда-либо делали одни и те же вещи. Мне действительно нравится этот вопрос. Спасибо за ответ. – NemeSys

+0

Пожалуйста, убедитесь, что вы всегда в одной теме ... – fbernardo

ответ

3

изменить аргументы передаются в обоих методах, как показано на рисунке,

[self performSelector:@selector(SampleMethod) withObject:self afterDelay:delayTime];  
[NSObject cancelPreviousPerformRequestsWithTarget:self];  

 Смежные вопросы

  • Нет связанных вопросов^_^