2011-07-20 104 views
4
- (void) startLoading { 

    [self blink]; 
} 

- (void) blink { 

     [UIView animateWithDuration:0.5 
           delay: 0.0 
          options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseOut 
         animations:^{ 
          //animate stuff 
         } 
         completion:^(BOOL finished){ 
           [self blink];  
         }]; 

} 

- (void) stopLoading { 
    //What goes here? 
} 

在我UIView的initWithFrame,我建立了一些裝載圖形然後從[self startLoading]啓動加載程序動畫。取消塊的UIView animateWithDuration

現在的問題是,我該如何阻止這種'無限循環'?stopLoading或dealloc方法能夠很好地分離所有東西嗎?

當我忽視的事實是一個完成塊是存在的,只是從超視圖釋放我UIView,一切順利幾秒鐘(比規定的0.5秒以上),則應用程序崩潰與消息:

malloc: * mmap(size=2097152) failed (error code=12) error: can't allocate region ** set a breakpoint in malloc_error_break to debug

我在malloc_error_break中有一個斷點,罪魁禍首就是動畫塊。

我假設UIView是通過從超級視圖中刪除而釋放的,稍後在完成塊被執行時,參考self這是消息發佈的對象。

我在文檔中找不到任何關於取消「排隊」塊的內容。

回答

7

要取消,你將與做你希望能夠取消任何循環操作:設置您之前循環每次檢查的標誌。所以:

- (void) stopLoading { 
    kCancel = YES; 
} 

現在你完成塊看起來是這樣的:

completion:^(BOOL finished){ 
    if (!kCancel) 
     [self blink];  
}]; 

kCancel可能是伊娃,靜態全局變量,等等。

+0

再次感謝馬特。我慢慢的開始關注動畫塊的事情 - 尤其是它不是'免費'的內存/ CPU。 – RickiG

+0

這種方法有一個不便之處。如果在飛行動畫塊完成之前將NO設置爲標記kCancel,則會再次調用閃爍方法。小心它。我希望有一種方法可以像cancelPreviousPerformRequestsWithTarget一樣永久取消塊,但顯然沒有辦法。 – erkanyildiz

+0

小點,但'kVariable'命名約定應該只能用於真正的靜態值 - 例如,類中按鈕的標準寬度等。這些通常實際上是用'const'參數聲明的。 - 相反,只需使用描述性變量名稱即可。 – toblerpwn

1

UIViewAnimationOptionRepeat具有魔法ü

[UIView animateWithDuration:0.5 
         delay: 0.0 
        options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionRepeat 
       animations:^{ 
         //animate stuff 
       } 
       completion:^(BOOL finished){ 
        NSLog(@"solved ");  
       }]; 
+1

謝謝:)但是,動畫塊外的眨眼法我增加一個變量。如果我使用repeat選項,則只會調用animate塊一次(然後在其他位置重複),並且完成塊不會被調用。所以據我所知,我需要使用「遞歸」的方法。除非我錯了,當然:) – RickiG

0

我重複剛纔的動畫和一些時間與調度塊後殺死它:

- (void)animate // Blink a view three times 
{ 
    // We need to stop the animation after a while (0.9 sec) 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.9 * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void) 
    { 
     [view.layer removeAllAnimations]; 
     view.alpha = 0.0; // Animation clean-up 
    }); 

    [UIView animateWithDuration:0.15 // 0.15*6=0.9: It will animate six times (three in reverse) 
          delay:0.0 
         options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat 
        animations:^{ 
         view.alpha = 1.0; // Animation 
        } 
        completion:NULL]; 
} 
相關問題