2010-10-13 128 views
0

我正在做一些使用UIView動畫和presentModalViewController的動畫。通過模擬器,它看起來很好,但在設備上它很波濤洶涌。即使相當基本的視圖,例如帶UISearchBar的viewController,具有自定義顏色和導航欄上的按鈕的UITableView(空),在通過presentModalViewController進行動畫處理時也會變得不穩定。UIView動畫性能問題

我試過在應用程序加載時將視圖加載到內存中,然後在按下按鈕時顯示它以查看是否有差異,但其結果相同。作爲一個說明我創建的代碼中的對象,並將它們添加到viewControllers視圖,我不使用筆尖。

有幾個意見,我所有的動畫是一個任務欄或一個alpha屬性,這些動畫罰款。

任何可能有用的技巧?這是否會在實際發佈應用程序時得到解決?

+0

你沒有在這些中實現drawRect嗎? – Nimrod 2010-10-13 00:37:14

+0

不是簡單的。我有一兩個人,但我已經使用drawRect動畫罰款。 – Rudiger 2010-10-13 00:40:55

回答

2

許多UIKit對象呈現速度很慢(如UITextViews和UISegmentedControllers),因此如果您嘗試一次對很多UIKit對象進行動畫製作,它們可能會看起來不連貫。動畫的每一幀都需要重繪視圖。

您可以通過將這些慢速視圖轉換爲UIImageViews來加速該過程,這些UIImageView可以更加平滑地動畫。嘗試在做動畫之前調用以下方法:

[self flattenView:yourView forDuration:0.5]; 


-(void)flattenView:(UIView *)view forDuration:(CGFloat)duration 
{ 
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageView * imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)]; 
    imgV.image = img; 
    [view addSubview:imgV]; 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 
     [imgV removeFromSuperview]; 
    }); 
}