yesimarobot
yesimarobot

Reputation: 283

iPhone Swapping out content of UIView/Sub view

I'm trying to accomplish the following:

Swap out the content of a UIView or Subview using UIButtons or a toolbar. I have a view laid out like the illustration below. I want to swap out the content of the "UIView to swap" sub view by clicking on buttons or buttons on a toolbar. So you click button 1 and a view is loaded into the "UIView to swap". Does this make sense? Can anyone suggest any tutorials? I know how to do this with images, but I want to swap more complicated views.

alt text http://yesimarobot.com/images/swap-uiviews.png

Upvotes: 3

Views: 4267

Answers (4)

Kris Van Bael
Kris Van Bael

Reputation: 2862

The answers above will work, but it is more elegant to have 4 viewcontrollers instead of 4 views. That gives you the option to make the actual views only when they are called for, and get rid (unload) some of the hidden ones in case the device is running short of memory.

(You're basically reinventing apple's UITabBarController, that one uses viewcontrollers too)

Upvotes: 0

Daniel Thorpe
Daniel Thorpe

Reputation: 3931

What you should do is create a container view, and add all your image views as subview, but set their hidden property to true except for the one you want to show first.

Create a CATransition with the desired animation (such as fade, push, move etc). Add the transition to the container view's layer.

Then to show a different view, just toggle the hidden properties of the two subviews, and the transition will animate the change for you.

The CATransition will look something like this:

// Create a transition
CATransition *transition = [CATransition animation];
transition.duration = 0.25f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
transition.delegate = self; 

// Add the transition to the content view's layer
[self.view.layer addAnimation:transition forKey:nil];

Upvotes: 1

Stefan Arentz
Stefan Arentz

Reputation: 34945

Makes sense. What about using UIView's replaceSubview:with: method to do that?

If your views are not very heavy then you can also simply keep all 4 ready in the super view and simply hide/show the appropriate ones.

Upvotes: 0

David Kanarek
David Kanarek

Reputation: 12613

You can do this very easily. Here's what I'd do:

1) Create a UIView to hold the swapped views. It should be the size of and in the location of UIView to swap in your picture. I will refer to it as container.

2) Construct your four UIViews that you want to swap between. Put them in an NSArray. I'll call it viewArray

3) Hook your buttons up to an IBAction method. This method should figure out the index of the button pushed (by name or tag), remove all of container's subviews, add the subview [viewArray objectAtIndex:myIndexFromButton] to container.

4) You're done. You can obviously create the views on demand instead of caching them if you like, but this is the general idea.

Upvotes: 2

Related Questions