UIView Animation将视图重置为原始rect

时间:2012-08-07 01:38:48

标签: ios animation uiview uiwebview cgrect

当点击链接时,我正试图通过右边框将UIWebView设置为屏幕上的动画。为了做到这一点,我使用了两个UIViews。一个叫做onScreenWebView,另一个叫做offScreenWebView。我的想法是,我可以将屏幕外的WebView从右侧设置到屏幕上,并将屏幕上的onScreenWebView动画到左侧。然后交换视图(因此屏幕上的视图变为onScreenWebView,反之亦然)但我的动画有问题。我必须注意它第一次很好用。之后它根本不起作用。

视图是这样对齐的

      __________ __________
      |        | |        |
      |   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视图与经典的临时变量交换交换。此外,他们是兄弟姐妹的观点。

2 个答案:

答案 0 :(得分:3)

我知道这有点旧,但我遇到了同样的问题,这是我发现的唯一一个线索。

问题是bringSubviewToFront执行隐式动画(我猜),阻止动画块分配正确的帧。

我通过禁用隐式动画解决了这个问题。 尝试使用这段代码而不是简单的bringSubviewToFront。

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

答案 1 :(得分:1)

我的一个应用程序中有类似的设置。不幸的是,我看不到你的实现有什么严重错误(虽然我们看不到你的交换和日志记录方法)。我不会呼唤其他方法或使用框架做多少工作。我确实遇到了临时变量交换的问题,因此恢复使用下一个视图控制器作为方法的参数,它工作得更好。

centreOffRightcentreOffLeft是CGPoints,分别定义屏幕左右偏移的中心。 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;}];

}

我不愿意将此作为答案发布,因为我无法理解为什么当你的答案没有时这会起作用,但希望它可以帮到你。