我正在尝试在链接被点击时从右边缘动画化UIWebView出现在屏幕上。为了实现这一点,我使用了两个UIView,一个名为onScreenWebView,另一个名为offScreenWebView。我的想法是可以将offscreenWebView从右边动画地移到屏幕上,并将onScreenWebView沿左侧移出屏幕。然后交换视图(使屏幕上的视图成为onScreenWebView,反之亦然)。但是我的动画存在问题。我必须注意它第一次运行得非常好。之后它就不起作用了。
这些视图对齐如下:
这是动画代码:
这是我的logWebRects方法的输出结果(仅包含每个视图的原点)。
这些视图对齐如下:
__________ __________
| | | |
| 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 视图。此外,它们是兄弟视图。