左右滑动 UIView
我正在尝试将 UIView 设置为左右滑动的动画。我尝试过的是截取当前视图的屏幕截图,然后在屏幕外更新视图内容时用屏幕截图替换视图,然后执行动画。但屏幕截图并未显示在屏幕上。在原始视图滑回到屏幕上之前,它只是黑色的。这是我正在使用的代码:
UIGraphicsBeginImageContext(self.view.window.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
iv.image = image;
[self.view.window insertSubview:iv aboveSubview:self.view];
self.view.frame = CGRectMake(-self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
iv.frame = CGRectMake(self.view.frame.size.width*2, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self loadData];
self.view.frame = CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
} completion:NULL];
[iv removeFromSuperview];
[iv release];
I'm trying to animate a UIView to slide left and right. What I've tried is to take a screenshot of the current view, than replace the view with the screenshot while updating the contents of the view offscreen, then doing the animation. But the screenshot isn't showing up on the screen. It's just black until the original view slides back onto the screen. Here's the code I'm using:
UIGraphicsBeginImageContext(self.view.window.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
iv.image = image;
[self.view.window insertSubview:iv aboveSubview:self.view];
self.view.frame = CGRectMake(-self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
iv.frame = CGRectMake(self.view.frame.size.width*2, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self loadData];
self.view.frame = CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
} completion:NULL];
[iv removeFromSuperview];
[iv release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了问题。我只需在完成块中执行
[iv removeFromSuperview];
即可。现在它正在发挥作用。I found the problem. I just had to do
[iv removeFromSuperview];
in the completion block. Now it's working.通常,您的应用程序中只需要一个窗口,即包含所有视图的窗口,因此我认为 self.view.window 不是一个好方法......
如果我得到了您想要的内容尝试实现,也许容器视图可能是正确的解决方案。
例如,假设您正在使用 iPhone:
现在您的 containerView 包含三个视图。向左或向右移动您的 containerView 以显示您喜欢的视图并隐藏其他两个视图,然后更新屏幕外的视图。
另一种方法是使用 UIScrollView 作为容器视图。
希望这有帮助...
编辑:我误解了你的问题,抱歉。
下一步尝试...
当您添加屏幕截图时,将其添加到屏幕右侧(屏幕外)
然后将原始视图移动到左侧(屏幕外)
但是您不显示屏幕截图,因此视图是空白的。
我认为你必须将其更改
为此
或者你可以添加一个动画以从右侧显示它。
Usually you need just one window in your app, the one containing all your views, so
self.view.window
is not a good approach, I think...If I've got what you're trying to achieve, maybe a container view could be the right solution.
For example, supposing you are working on an iPhone:
Now you have your containerView containing three views. Move your containerView left or right to show the view you like and to hide the other two views, then update the views that are out of the screen.
Another approach could be using UIScrollView as containerView.
Hope this helps...
EDIT: I've misunderstood your question, sorry.
Next try...
When you add your screenshot, you add it on the right of the screen (offscreen)
Then you move you original view on the left (offscreen)
But you don't show your screenshot, so the view is blank.
I think you have to change this
To this
Or you can add an animation to show it from the right.