3

当点击链接时,我正在尝试从右边框将 UIWebView 动画到屏幕上。为了做到这一点,我使用了两个 UIView。一个叫onScreenWebView,另一个叫offScreenWebView。这个想法是我可以将 offscreenWebView 从右侧设置到屏幕上,并将 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 视图。此外,它们是兄弟视图。

4

2 回答 2

3

我知道这有点老了,但我遇到了同样的问题,这是我发现的唯一一个关于它的线程。

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

我通过禁用隐式动画来解决它。尝试使用这段代码而不是普通的bringSubviewToFront。

[CATransaction begin];
[CATransaction setDisabledActions:YES];
[self bringSubviewToFront:offScreenWebView];
[CATransaction commit];
于 2012-10-17T09:27:31.630 回答
1

在我的一个应用程序中,我的设置与您的设置类似。不幸的是,我看不到您的实现有任何明显错误(尽管我们看不到您的交换和日志记录方法)。我的没有调用其他方法或使用框架做太多工作。我确实遇到了临时变量交换的问题,因此恢复为使用下一个视图控制器作为方法的参数,它工作得更好。

centreOffRightcentreOffLeft是分别定义屏幕中心向左和向右偏移的 CGPoints。next并且previous是每个视图的视图控制器。从容器视图控制器调用此方法。

-(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;}];

}

我不愿意将此作为答案发布,因为我不明白为什么当您的答案不起作用时这会起作用,但希望它对您有所帮助。

于 2012-08-08T06:09:38.267 回答