2013-02-22 168 views
0

我需要動畫UIimageview,當我開始動畫它發生很長時間後,我發現內存泄漏的地方在各種圖像視圖的連續動畫的時間。我需要做的就是在滾動視圖中有n個UIviews,每個視圖都有一個自定義按鈕。選擇該按鈕時的動畫時,如果動畫是在過程中如果用戶點擊按鈕,該動畫將繼續下去,它不需要重新開始UIImageview動畫開始使用for-in循環和動畫延遲

這裏是我的代碼

for (int i=0; i<AlphabetArray.count; i++) { 

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)]; 
    view.backgroundColor=[UIColor grayColor]; 
    view.tag=i+1; 
    [self.scrollView addSubview:view]; 
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(200, 50, 400, 400)]; 


    [image setImage:[[animationArray objectAtIndex:i]objectAtIndex:0]]; 
    image.tag=i+1; 
    [view addSubview:image]; 

    UIButton *animatebutton=[[UIButton alloc]initWithFrame:CGRectMake(200, 50, 336, 310)]; 
    animatebutton.tag=i+1; 
    [animatebutton addTarget:self action:@selector(makeAnimation:) forControlEvents:UIControlEventTouchUpInside]; 
    //[animatebutton setImage:[UIImage imageNamed:@"NewtDance_mov_0.png"] forState:UIControlStateNormal]; 
    [view addSubview:animatebutton]; 
} 

-(void)makeAnimation:(UIImageView *)sender { 

UIView *tagView=(UIView *)[self.view viewWithTag:sender.tag]; 



for (UIImageView * imageview in [tagView subviews]) { 

    NSLog(@"Yes %d",imageview.tag); 


    if ([imageview isKindOfClass:[UIImageView class]]) { 

     if ([imageview isAnimating]) { 
      NSLog(@"Animation Happens"); 

     } 

     else{ 
      imageview.animationDuration=3.0; 

      imageview.animationImages=[animationArray objectAtIndex:sender.tag-1]; 
      imageview.animationRepeatCount=2; 
      imageview.tag=sender.tag; 
      [imageview startAnimating]; 
     } 
    } 

} 



NSLog(@"OK"); 


} 

有了這個代碼我可以達到我的要求。但動畫開始時間很晚,因爲使用了for-in循環

+0

確保你的動畫是完美的。然後減少動畫持續時間並檢查它與斷點也。 – Suresh 2013-02-22 10:59:00

+0

@Suresh我試圖減少動畫的持續時間仍然面臨相同 – 2013-02-22 11:06:04

+0

你檢查斷點?它實際上是什麼?你的動畫是完美的嗎? – Suresh 2013-02-22 11:07:55

回答

5

處理此問題的最佳方法。使用UIImageViewanimationImages財產。

  1. 請使用您的圖片的UIImage對象NSMutableArray對象。
  2. 設置圖像陣列的UIImageViewanimationImages財產
    [imageView setAnimationImages:imagesArray]

  3. [imageView startAnimating]啓動動畫。

  4. [imageView stopAnimating]停止動畫。
    查看文檔瞭解更多詳情。

欲瞭解更多詳情,check this.

+0

該文件聽到。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html – damithH 2013-02-22 11:05:37

+0

我不明白你的答案 – 2013-02-22 11:06:22

+0

@damithH現在你只需要刪除你的第二個答案。 – rptwsthi 2013-02-22 11:44:15