bruno
bruno

Reputation: 2169

Update view of a NavigationController simulating a push

Can i make a push of a view with the animation and only update it´s contents? I have 4 views that are the same, so instead of creating 4 xibs, i wanted to update the view but give the user the impression that he had switched views.

So i don´t really want to make a push, i only want to give that impression to the user. Is this possible?

Upvotes: 1

Views: 924

Answers (3)

ader
ader

Reputation: 5393

The best approach to this (imho) is to pop the current view without animation and then immediately push the same view again.

myNavigationController = self.navigationController;
[myNavigationController popViewControllerAnimated:NO];
[myNavigationController pushViewController:myViewController animated:YES];

This is part of the technique I use to give the effect of an unlimited number of views whilst in reality only using/keeping one in memory.

When wanting to handle the "back" navigation you would push a new one without animation and then pop it with animation.

myNavigationController = self.navigationController;
[myNavigationController pushViewController:myViewController animated:NO];
[myNavigationController popViewControllerAnimated:YES];

Upvotes: 4

jmstone617
jmstone617

Reputation: 5707

I set up a simple project with one VC and button in the middle to trigger the animation. Here is the code tied to the button:

- (IBAction)pushNewView:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
    // this moves the view off screen to the left
    self.view.frame = CGRectMake(-320, 0, 320, 480);
} completion:^(BOOL finished) {
    // this pops the view over to the right side of the screen
    self.view.frame = CGRectMake(320, 0, 320, 480);
    [UIView animateWithDuration:0.5 animations:^{
        // and this slides it in from the right
        self.view.frame = CGRectMake(0, 0, 320, 480);
    }];
}]; }

You can play around with the timing to make it look a little more "native", and play with the alpha as previously mentioned. You can alter this in either of the two animation blocks.

Upvotes: 1

wattson12
wattson12

Reputation: 11174

you can animate one view to the left and at the same time animate the next view from the right, this will be the same as a push (assuming next and current views are already subviews)

UIView *nextView.frame = CGRectOffset(self.view.frame, self.view.frame.size.width, 0.0f);

[UIView animateWithDuration:0.3f animations:^{
    currentView.frame = CGRectOffset(currentView.frame, -self.view.frame.size.width, 0.0f);
    nextView.frame = CGRectOffset(nextView.frame, -self.view.frame.size.width, 0.0f);
}];

it would probably look better if you experimented with changing the alpha property of the views as well

Upvotes: 0

Related Questions