2014-02-17 62 views
0

在我的項目中,我實現了一個水平滾動文本的股票動畫。UIViewAnimation增加內存

我的問題是當我去另一個viewcontroller內存開始不斷增加。

這裏是我的股票動畫代碼

-(void)scrollTheBreakingNews 
{ 

if (isTicker) 
{ 
    self.ticker.text = textToScroll; 

    if (!pauseTicker) 
    { 
     if (isTicker) 
     { 
      NSAttributedString *str = [[NSAttributedString alloc]initWithString:textToScroll]; 

      CGSize textSize = [str size]; 

      if (isTicker) 
      { 
       float duration = (textSize.width + self.tickerView.frame.size.width)/65.0f; 

       float startingX=0.0f; 
       float endX=0.0f; 

       if (isTicker) 
       { 
        self.ticker.frame = scrollLabelFrame; 
        if (isTicker) 
        { 
         startingX = self.tickerView.frame.size.width; 
         endX = -textSize.width; 
         if (isTicker) 
         { 
          self.ticker.frame = CGRectMake(startingX, 0.0f, textSize.width, 25.0f); 

          [UIView beginAnimations:@"" context:nil]; 
          [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
          [UIView setAnimationDuration:duration]; 
          [UIView setAnimationDelegate:self]; 
          [UIView setAnimationDidStopSelector:@selector(tickerStop)]; 

          if (isTicker) 
          { 
           CGRect tickerFrame = self.ticker.frame; 
           tickerFrame.origin.x = endX; 
           if (isTicker) 
           { 
            [self.ticker setFrame:tickerFrame]; 
            [UIView commitAnimations]; 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

} 

-(void)tickerStop 
{ 
    if (isTicker) 
    { 
     if (!pauseTicker) 
     { 
      [self scrollTheBreakingNews]; 
     } 

    } 
    else 
    { 
     textToScroll=nil; 
    } 
} 

記憶是這樣的:

請幫我解決這個問題。任何建議感激。在此先感謝

+0

'儀表中的配置文件'按鈕放置在那裏是有原因的,您可以使用它來查看佔用內存的內容。 –

+0

我對分析不太熟悉。我簡介了我的應用程序,但我找不到哪個對象正在使用更多內存 –

+2

這是非常直觀的工具,但您可以查看文檔以開始使用:[關於儀器](https://developer.apple.com/ library/ios/documentation/developertools/concepts/instrumentsuserguide/Introduction/Introduction.html),[在應用程序中查找內存問題](https://developer.apple.com/library/ios/documentation/developertools/conceptual/instrumentsuserguide/ MemoryManagementforYouriOSApp/MemoryManagementforYouriOSApp.html#// apple_ref/DOC/UID/TP40004652-CH11-SW1)。 –

回答

1

好的,第一件事......哇,你需要改進代碼風格。

讓我們走吧。

其次,停止使用舊式動畫代碼。該文檔甚至表示不會在iOS 4.0之後使用它。

-(void)scrollTheBreakingNews 
{ 
    //You are already checking isTicker here there is 
    //no reason to check it another SEVEN times inside this block. 
    if (isTicker) 
    { 
     self.ticker.text = textToScroll; 

     if (!pauseTicker) 
     { 
      NSAttributedString *str = [[NSAttributedString alloc] initWithString:textToScroll]; 

      CGSize textSize = [str size]; 

      float duration = (textSize.width + self.tickerView.frame.size.width)/65.0f; 

      float startingX=0.0f; 
      float endX=0.0f; 

      self.ticker.frame = scrollLabelFrame; 
      startingX = self.tickerView.frame.size.width; 
      endX = -textSize.width; 
      self.ticker.frame = CGRectMake(startingX, 0.0f, textSize.width, 25.0f); 

      CGRect tickerFrame = self.ticker.frame; 
      tickerFrame.origin.x = endX; 

      [UIView animateWithDuration:duration 
            delay:0.0 
           options:UIViewAnimationOptionsCurveLinear 
          animations:^(){ 
           self.ticker.frame = tickerFrame 
          } 
          completion:^(BOOL finished){ 
           [self tickerStop]; 
          }]; 
     } 
    } 
} 

-(void)tickerStop 
{ 
    if (!pauseTicker 
     && isTicker) { 
     [self scrollTheBreakingNews]; 
    } 
    else { 
     textToScroll=nil; 
    } 
} 

至於內存問題。我建議通過使用工具分析應用程序來找出代碼的哪一部分導致問題。

您可能會發現這會改善內存使用情況嗎?也許,但不是100%確定的。

+0

好吧..讓我試試你的片段... :-),我承認我的編碼風格很差:-(我肯定會改進:-) –

+0

不用擔心。如果沒有其他,它會使您的應用程序的維護更容易。通過8種不同的if語句來檢查相同的事情,無疑是增加了複雜性。 – Fogmeister

+0

@MusadhikhMuhammedK請注意,我剛剛在動畫塊之前刪除了一行。如果您複製/粘貼我的代碼,您也需要這樣做。 – Fogmeister

0

也許我不應該批評,但編碼風格不好,它可能會在未來造成很多麻煩。我猜你試圖停止動畫,如果isTicker設置爲false可能由於用戶操作。

只需在動畫之前檢查一次isTicker的值即可。要檢查它的價值區間是如此之小,無論哪種方式

if (isTicker) 
{ 
    float duration = (textSize.width + self.tickerView.frame.size.width)/65.0f; 
    float startingX=0.0f; 
    float endX=0.0f; 

    if (isTicker) 

這將需要您的設備幾毫秒到這裏例如兩次檢查isTicker的價值。

你絕對應該改變編碼風格,我對增加的內存消耗的猜測是下面的代碼片段。您開始動畫,但如果isTicker設置爲FALSE,動畫未提交,則會創建動畫上下文,但尚未完成,因此可能仍存儲在內存中。

[UIView beginAnimations:@"" context:nil]; 
         [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
         [UIView setAnimationDuration:duration]; 
         [UIView setAnimationDelegate:self]; 
         [UIView setAnimationDidStopSelector:@selector(tickerStop)]; 

         if (isTicker) 
         { 
          CGRect tickerFrame = self.ticker.frame; 
          tickerFrame.origin.x = endX; 
          if (isTicker) 
          { 
           [self.ticker setFrame:tickerFrame]; 
           [UIView commitAnimations]; 
          } 
         }