我正在尝试动画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];
答案 0 :(得分:2)
我发现了问题。我只需要在完成块中执行[iv removeFromSuperview];
。现在它正在发挥作用。
答案 1 :(得分:-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)];
或者您可以添加动画以从右侧显示。