How to Swap View in Iphone?

There is one way of handling autorotation, and it’s an option you’ll likely only use in the case of very complex interface. We just design the landscape and portrait views separately and then swap them out when the phone is rotated.

 There is one way  of handling autorotation, and it’s an option you’ll likely only use in the case of very complex interface. We just design the landscape and portrait views separately and then swap them out when the phone is rotated.

Step 1: Create a new project in Xcode using the view-based application.

Step 2: Double click .xib file to open the file in interface Builder. First single click View icon, and press delete botton. Next drag over two UIView from library . Single click the view to select it and change name one view Portrait and other Landscape. Now , control drag from File’s Owner to the Portrait icon and select portrait outlet. Then, control drag from File’s Owner a second time to the Landscape icon, and select Landscape outlet. Double click the icon called Landscape, and bring up size inspector. Change the view value 480 pixel wide and 300 pixels tall. Now drag two Round Rect Buttons over from the library to the Landscape view. Double click the left button, and give the title Foo, and then double click the right one , and give it a title of Bar. Control drag from the File’s Owner icon to the Foo button, and assign it to the landscapeFoo outlet, and then do the same thing to assign the Bar button to the landscapeBar outlet. Single click Foo button, and bring to connection inspector. Drag touch up inside to File’s Owner icon. (See figure 1)

Figure 1: Landscape View.

Step 3: We need to put the code in place to handle the swap and the button taps.

-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:
  (UIInterfaceOrientation)toInterfaceOrientation
   duration:(NSTimeInterval)duration
{
        //UIInterfaceOrientation toOrientation = self.interfaceOrientation;
       
        if(toOrientation == UIInterfaceOrientationPortrait)
        {
                self.view = self.portrait;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform =
                CGAffineTransformMakeRotation(degreesToRadian(0));
                self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
        }

        else if(toOrientation == UIInterfaceOrientationLandscapeLeft)
        {
                self.view = self.landscape;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform =
                CGAffineTransformMakeRotation(degreesToRadian(-90));
                self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
        }
       
        else if (toOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
                self.view = self.portrait;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform =
                CGAffineTransformMakeRotation(degreesToRadian(180));
                self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
        }
       
        else if(toOrientation == UIInterfaceOrientationLandscapeRight)
        {
                self.view = self.landscape;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform =
                CGAffineTransformMakeRotation(degreesToRadian(90));
                self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
        }
       
  }

Step 4:  The core graphics framework is not included in our Xcode project. We  have to manually link that framework into our project. The iPhone OS frameworks can be found here : 
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/System/Library/Frameworks

Step 5: Now compile and rum it on the simulator.(See figure 2)

Figure 2: Portrait View.

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG