1
votes

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

2 Answers

2
votes

I found the problem. I just had to do [iv removeFromSuperview]; in the completion block. Now it's working.

-1
votes

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:

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

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)

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

Then you move you original view on the left (offscreen)

    self.view.frame = CGRectMake(-self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);

But you don't show your screenshot, so the view is blank.

I think you have to change this

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

To this

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

Or you can add an animation to show it from the right.