2012-01-29 141 views
1

我正在尝试动画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]; 

回答

2

我发现了这个问题。我只需要在完成块中做[iv removeFromSuperview];。现在它正在工作。

+0

你能发布最终的代码吗?我遵循你的建议,但仍然遇到你描述的问题(截图没有出现)。 – user664939 2014-04-21 17:43:52

+0

@ user664939我真的不知道我用过这个。但从我的回答判断,你可以添加一个完成块来删除视图。 – edc1591 2014-04-21 23:46:37

+0

感谢您回复我的评论。基于这个和其他一些问题,我能够得到它的工作。 – user664939 2014-04-22 00:05:28

-1

通常你只需要一个窗口,在您的应用程序,包含所有你的观点的人,所以self.view.window是不是一个好办法,我想......

如果我有你想要实现的,也许一个容器视图可能是正确的解决方案。

例如,假设您正在使用的iPhone手机:

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 960.0f, 480.0f)]; 

firstView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f); 
secondView.frame = CGRectMake(320.0f, 0.0f, 320.0f, 480.0f); 
thirdView.frame = CGRectMake(640.0f, 0.0f, 320.0f, 480.0f); 

[containerView addSubview:firstView]; 
[containerView addSubview:secondView]; 
[containerView addSubview:thirdView]; 

现在你有一个包含三个视图您containerView。向左或向右移动containerView以显示您喜欢的视图并隐藏其他两个视图,然后更新屏幕外的视图。

另一种方法是使用UIScrollView作为containerView。

希望这有助于...

编辑:我误解你的问题,对不起。 接着再试试......

当你把你的截图,你加它的屏幕(屏幕外)

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); 

但是你不显示你的截图,所以视图是空白的。

我认为你必须要改变这种

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)]; 

对此

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)]; 

或者你也可以添加动画从右侧显示它。

+0

就像我在我的问题中所说的,我只使用两个视图,而不是三个视图。这个想法是我想重用self.view。这就是我使用图像视图的原因。 – edc1591 2012-01-29 19:54:14

+0

而我只使用一个窗口。 self.view.window是应用程序的主窗口。 – edc1591 2012-01-29 19:56:40

+0

啊......我看到......我在想你的问题是左右滑动(看你的标题),而不是显示你的截图。看看我的编辑。 – Beppe 2012-01-29 21:03:06