Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2091279
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类:

2010-04-25 15:13:57

  1. Introduction
    Allow application to be used in either portrait or landscape mode and to change orientation at runtime if phone is rotated. You can configure to only support one orientation.
    Autorotate is specified in the view controller, so if the user rotate the phone,  the view controller will be asked if it's OK to rotate to the new orientation, if the view controller responds in the affrimative, the application's window and views will be rotated, and the window and views will get resized to fit the new orientation.
  2. How to Enale Autorotate
    There are 3 approaches to implement autorotate. For the 2nd and 3rd approach,  you need to override methods from UIViewController in your view's controller class.
    1. Set correct autosize attributes for all obects make up your UI
    2. Manuall reposition the objects in your view when notified that your view is rotated
    3. Design 2 different versions of your view in Interface Builder, one for portrait mode, and a seperat one for landscape mode.
  3. Rorate using autosize attribute
    • Tell iPhone that our view supports autorotate
      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
          //Return YES for supported orientations
          return
      (interfaceOrientation == UIInterfaceOrientationPortrait);
      }
      When the phone is changed to the new orientation, this method is called on the active view controller.
      There are 4 defined orientations:
      • UIInterfaceOrientationPortrait
      • UIInterfaceOrientationPortraitUpsideDown //This orientation is discurraged, because if the phone rings while it is being up held upside down, the phone likely to remain upside down when it is answered.
      • UIInterfaceOrientationLandscapeLeft 
      • UIInterfaceOrientationLandscapeRight
    • Set correct autosize attribute
      Make object in IB to get focus, then pres Command-3 to bring up the size inspector, on the inspector, you can set autosize attribute.
    • ...
  4. Reoperation Manually
    Move the objects on rotation.

    - (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
    {
        [UIView beginAnimations:@"move buttons" context:nil]; //Support animation
        if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
        {
            ....
            button.frame = CGReceMake(xxxxxx);
        }
        else
        {
            ....
            button.frame = CGReceMake(xxxxxx);
        }
        [UIView commitAnimations];
    }
  5. Design 2 different versions
    • In your Nib, add 2 view, one is for portrait mode, and another is for landscape mode.
    • Add outlet  and establish connection
      IBOutlet UIView *pViewLandscape;
      IBOutlet UIView *pViewPortrait;
    • Set correct view
      - (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      {
          if (toOrientation == UIInterfaceOrientationPortrait)
          {
              self.view = self.pViewPortrait;
          }
          else
          {
             self.view = self.pViewLandscape;
          }
      }
    • On event handler, you can determine which object trigger the event according sender.
  6. ...

Reference:
  1. Beginning iPhone Development: Exploring the iPhone SDK
    Chapter: Application Settings and User Defaults
    Page: P89~....
     
阅读(1063) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~