2009-10-07 70 views
0

我是iPhone新手,我試圖在UIView和另一個包含常規UIView和UIScrollView的UIView之間做翻轉動畫,依次滾動視圖有幾個UIViews作爲子視圖。在UIView和UIScrollView之間做翻轉動畫的問題

在動畫開始之前,滾動視圖需要偏移到特定的點以顯示特定的子視圖(用戶跳轉到滾動視圖中的特定「章節」)。

它動畫很好,但問題在於它會'有時'(可能每隔三次)使用滾動視圖的子視圖之一啓動動畫,而不是使用原始UIView啓動動畫('overlayView'在下面的代碼中)。我懷疑這與在動畫之前設置滾動視圖的偏移量有關。

這是我目前要做的事:

// get the MPMoviePlayer window that has the views to animate between as subviews 
    UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow]; 

    // tell the controller of the scroll view to set the scroll view offset 
    [instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]]; 

    // Animate transition to instruction view 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1.5]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO]; 

    // the views to animate between 
    [moviePlayerWindow sendSubviewToBack: overlayView]; 
    [moviePlayerWindow bringSubviewToFront: instructionControlsController.view]; 

    [UIView commitAnimations]; 

控制器中setInstructionImageWithNumber方法是這樣的:

- (void) setInstructionImageWithNumber:(int)number 
{ 
    if (number < kNumberOfPages) 
     [scrollView setContentOffset: CGPointMake((number * kImageWidth), 0) animated:NO]; 
} 

什麼,我可能是做錯了任何想法,爲什麼我得到這個行爲動畫有時看起來很好,有時甚至沒有?

回答

2

如果您給運行循環機會在beginAnimations之前更新視圖,會發生什麼情況?您可能需要這樣做才能讓視圖有機會「趕上」並在動畫開始之前進行精確更新。

UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow]; 
[instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]]; 

//Before continuing the animation, let the views update 
[self performSelector:@selector(continueTheAnimation:) withObject:nil afterDelay:0.0]; 

。 。 。

- (void)continueTheAnimation:(void*)context { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1.5]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO]; 
    [moviePlayerWindow sendSubviewToBack: overlayView]; 
    [moviePlayerWindow bringSubviewToFront: instructionControlsController.view]; 
    [UIView commitAnimations]; 
} 
+0

這樣做的伎倆,謝謝!我會再看一下performSelector - 看起來他們在將來也可以非常方便。 – Cactuar

+0

原來我很快就說過了,這個錯誤仍然存​​在 - 但現在似乎更少出現,而且它在模擬器中從未發生過。我會繼續調查... – Cactuar

+0

因爲它在模擬器中從來沒有發生過,所以我不知道該設備是否需要更多時間來準備(或清理)所有東西。如果你改變afterDelay會發生什麼:從0.0到0.1甚至更高? – Rob