2012-08-07 42 views
3

我試圖在點擊鏈接時將UIWebView的動畫從右邊框移動到屏幕上。爲了做到這一點,我使用了兩個UIViews。一個叫onScreenWebView,另一個叫offScreenWebView。我的想法是,我可以將offscreenWebView製作爲右側屏幕的動畫,並將屏幕上的onScreenWebView製作爲左側動畫。然後交換視圖(所以屏幕上的視圖變成了onScreenWebView,反之亦然),但是我的動畫有問題。第一次我必須注意它很好用。之後,它不能很好地工作。UIView動畫將視圖重置爲原始的矩形

的意見對齊,這樣

 __________ __________ 
     |  | |  | 
     | on | | off | 
     | screen | | screen | 
     |________| |________| 

這裏的動畫代碼:

offScreenWebView.hidden = true; 

offScreenWebView.frame = self.frame; 
[offScreenWebView offSetX:self.bounds.size.width + kOffsetPadding];   // move offscreen to the right 
[self logWebRects:@"begin"]; 
offScreenWebView.hidden = false; 
[self bringSubviewToFront:offScreenWebView]; 

[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationCurveEaseInOut animations:^{ 
    [self logWebRects:@"during 1"]; 
    offScreenWebView.frame = self.frame; 
    onScreenWebView.frame = CGRectMake(-(self.bounds.size.width + kOffsetPadding), 0, onScreenWebView.bounds.size.width, onScreenWebView.bounds.size.height); 
    [self logWebRects:@"during 2"]; 
}completion:^(BOOL finished) { 
    [self logWebRects:@"finished b4 swap"]; 
    [self swapWebViews]; 
    [self logWebRects:@"finished -- done"]; 
}]; 

這是我的logWebRects方法的輸出(每個視圖的只是原點)

Navigation Type ::  Link Clicked 
Rect of self frame :: {{0, 0}, {320, 460}} 

99 -- Point of offscreen origin begin   :: {335, 0} 
0 -- Point of onscreen origin begin   :: {0, 0} 
99 -- Point of offscreen origin during 1  :: {335, 0} 
0 -- Point of onscreen origin during 1  :: {0, 0} 
99 -- Point of offscreen origin during 2  :: {0, 0} 
0 -- Point of onscreen origin during 2  :: {-335, 0} 
Navigation Type ::  Other 
99 -- Point of offscreen origin finished b4 swap  :: {0, 0} 
0 -- Point of onscreen origin finished b4 swap  :: {-335, 0} 
0 -- Point of offscreen origin finished -- done  :: {-335, 0} 
99 -- Point of onscreen origin finished -- done  :: {0, 0} 


Navigation Type ::  Link Clicked 
Rect of self frame :: {{0, 0}, {320, 460}} 

0 -- Point of offscreen origin begin   :: {335, 0} 
99 -- Point of onscreen origin begin   :: {0, 0} 
0 -- Point of offscreen origin during 1  :: {335, 0} 
99 -- Point of onscreen origin during 1  :: {0, 0} 
0 -- Point of offscreen origin during 2  :: {0, 0} 
99 -- Point of onscreen origin during 2  :: {-335, 0} 
Navigation Type ::  Other 
0 -- Point of offscreen origin finished b4 swap  :: {335, 0} 
99 -- Point of onscreen origin finished b4 swap  :: {0, 0} 
99 -- Point of offscreen origin finished -- done  :: {0, 0} 
0 -- Point of onscreen origin finished -- done  :: {335, 0} 

這些日誌來自最初的運行。然後也是第二次運行。您會注意到,無論出於何種原因,動畫塊都會在完成塊之前重置每個Web視圖的框架。

我應該注意到,我將web視圖與經典的臨時變量交換交換。此外,他們是兄弟姐妹的意見。

回答

1

我在我的一個應用程序中有類似的設置。不幸的是,我看不出你的實現有什麼大的問題(儘管我們看不到你的交換和日誌記錄方法)。我不會向其他方法發出呼籲,也不要對框架做很多工作。我確實有一個臨時變量交換的問題,因此恢復爲使用下一個視圖控制器作爲方法的參數,它工作得更好。

centreOffRightcentreOffLeft是分別定義屏幕中心向左和向右偏移的CGPoint。 nextprevious是每個視圖的視圖控制器。此方法從容器視圖控制器中調用。

-(void)moveNext:(PlayerViewController*)sender 
{ 
    // Need to push this one from the right 
    next.view.center = centreOffRight; 
    [self.view bringSubviewToFront:next.view]; 
    [UIView animateWithDuration:0.3 
        animations:^{ 
         next.view.center = centreOfScreen; 
         current.view.center = centreOffLeft;} 
        completion:^(BOOL finished){ 
         [current wasMovedOffScreen]; 
         current = next; 
         next = sender; 
         currentDouble = nextIndex;}]; 

} 

我不願意張貼此作爲一個答案,我不明白爲什麼當你沒有這個會的工作,但希望它可以幫助你。

+0

感謝您的意見。我正在考慮用視圖控制器來實現這一點。我必須嘗試一下。 – atreat 2012-08-12 21:56:31

3

我知道這有點舊了,但我遇到了同樣的問題,這是我發現的唯一線索。

問題是,bringSubviewToFront執行一個隱式動畫(我猜),它阻止動畫塊分配正確的幀。

我解決了它通過禁用隱式動畫。 嘗試使用這段代碼而不是普通的bringSubviewToFront。

[CATransaction begin]; 
[CATransaction setDisabledActions:YES]; 
[self bringSubviewToFront:offScreenWebView]; 
[CATransaction commit];