How to animate switching of views?

This snippet deals with animating switching of views.

 

Normally when users have to switch views they simply add the following code

[self.view addSubview:someview];

and to remove this view from superview they use

[self.view removeFromSuperview];

The above code do not perform any animations while switching views. They simply switch.

Adding few lines of code can make switching of views more interesting.

While you add or remove views just do it inside following code to add life to the switching of views.

 

[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[self.view addSubview:someViewController.view];
[UIView commitAnimations];

Now lets see what these lines of code do.

1) begin Animations: start our animation block. You can give any name in place of @”flipping view”.

2) setAnimationDuration: sets the duration for which the animation will last.

3) setAnimationCurve: sets the curve of animating property changes with in an animation. You can use following constants in this place

➤    UIViewAnimationCurveEaseInOut —cause the animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing.

➤    UIViewAnimationCurveEaseIn — causes the animation to begin slowly and then speed up as it progresses

➤    UIViewAnimationCurveEaseOut — causes the animation to begin quickly and then slow as it completes

➤    UIViewAnimationCurveLinear — causes an animation to occur evenly over its duration

4) setAnimationTransition: applies a transition type to the animation. Following constants can be used.

➤ UIViewAnimationTransitionNone — no transition

➤ UIViewAnimationTransitionFlipFromLeft — flips a view around vertical axis from left to right

➤ UIViewAnimationTransitionFlipFromRight — flips a view around a vertical axis from right to left

➤ UIViewAnimationTransitionCurlUp — curls a view up from the bottom

➤UIViewAnimationTransitionCurlDown — curls a view down from the top

5) addSubView: is the usual method by which you add a subview to a view. Here someViewController is a class object whose view we want to add.

6) In the end we commit our animation block.

How to use UISegmentedControl in iPhone & iPad

UISegmentedControl in iPhone & iPad

 

So today we are going to create a simple app that is going to use UISegmentedControl . In our previous tutorials we created simple iPhone app. This time we are going to create an iPad application. The process and code is exactly same for both of these, we just need to initially select it in the Xcode.

 

Basically our app will have a menu with name of some colors written on each menu. Whenever a menu is selected, the color of the text is changed to that represented by the menu.

Given below is the snapshot of what will our app look like.

iPhone Application Development

 

So lets get started :-

1.) Creating a new project

The first step is to create a new project. For this we need to :-

a.) launch Xcode.
b.) click on File -> New Project.
c.) select Application (under iOS).
d.) select View-Based Application project template.
e.) Under Product , choose iPad.
f.) click on choose.
g.) save the project and  name it “menu”.

 

iPhone Application Tutorial

This is how the list of files look like in Xcode.

iphone Application Development

 

2.)Building the Interface

Go to “Resources” folder and click “menuViewController.xib” file. This will launch the “Interface Builder” which consists of few windows.

a.)Now we will go to the “Library window” and from there we will select the “Text View” and drag it to the “View window”.

b.)Same way we are going to drag  ”Segmented Control” .

c.) We will make the interface as shown in the figure and name the objects according to it.

iPhone Application Development

 

3.)Writing Code

a.)So Code for “menuViewController.h” file

There would be a piece of code that Xcode has already written for you. Here’s what the whole code will look like.

#import <UIKit/UIKit.h>

@interface menuViewController : UIViewController {
	IBOutlet UISegmentedControl *mycolors;
	IBOutlet UITextView *sampleText;

}

@property(nonatomic,retain) IBOutlet UISegmentedControl *mycolors;
@property(nonatomic,retain) IBOutlet UITextView *sampleText;

-(IBAction) colorChanged;

@end

Here we declare a  “SegmentedControl” object and a “TextView” object and set their property.

We declare a function “colorChanged” which when called will change the color of the “TextView.”

 

b.)Code for “menuViewController.m” file:

#import "menuViewController.h"

@implementation menuViewController

@synthesize mycolors,sampleText;

-(IBAction) colorChanged {

if (mycolors.selectedSegmentIndex==0)
{
sampleText.textColor = [UIColor blackColor];
}
if (mycolors.selectedSegmentIndex==1)
{
sampleText.textColor = [UIColor greenColor];
}
if (mycolors.selectedSegmentIndex==2)
{
sampleText.textColor = [UIColor blueColor];
}
if (mycolors.selectedSegmentIndex==3)
{
sampleText.textColor = [UIColor brownColor];
}
if (mycolors.selectedSegmentIndex==4)
{
sampleText.textColor = [UIColor redColor];
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)dealloc {
[mycolors release];
[sampleText release];
[super dealloc];
}

@end

Here we synthesize the “mycolors” and “sampleText” objects.

Now we define the “colorChanged” method. It checks for the index value of the  “SegmentedControl” and changes the color of  “sampleText” according to it.

 

4.)Building the Connections

Our next step is to build the connections between the code we have written with our Interface elements.

a.)So we click on the “menuViewController.xib” in the “Resources” folder to launch “Interface Builder.

b.)There in the “Document window” we select “File’s Owner” and right click it.

c.)Now a window pops up which shows all the “Outlets and Actions” defined by us.

d.)Do the connections as shown below.

iPhone Application Development

 

5.)Build and Run

Now finally we have to go to the Xcode and there at the top bar we see “Build and Run”. Just click it to run the application. Make sure we have saved our application before doing this.

iPhone Application Development

So finally our app will look something like this:-

iPhone Application Development