2013-04-12 42 views
1

因此,我在幾個視圖控制器中使用了以下代碼,以根據方向更改以不同方式繪製一些自定義對象。根據方向更改更改視圖iOS

出於某種原因,以下兩種方法的這種特定的實現不工作:

- (void)awakeFromNib 
{ 
    isShowingLandscapeView = NO; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(orientationChanged:) 
               name:UIDeviceOrientationDidChangeNotification 
               object:[UIDevice currentDevice]]; 
} 



- (void)orientationChanged:(NSNotification *)notification 
{ 
    NSLog(@"Are we changing orientations?"); 
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && 
     !isShowingLandscapeView) 
    { 
     isShowingLandscapeView = YES; 
     NSLog(@"Landscape!"); 

     /* 

     // Landscape orientation 
     [txtLoginID setFrame:CGRectMake(489, 200, 145, 30)]; 
     [txtPassword setFrame:CGRectMake(489, 235, 145, 30)]; 
     [loginLabel setFrame:CGRectMake(378, 203, 69, 21)]; 
     [pwdLabel setFrame:CGRectMake(378, 238, 80, 21)]; 
     loginButton.frame = CGRectMake(561, 273, 73, 30); 

     // Set iPad landscape background 
     background.frame = CGRectMake(0, 0, 1024, 748); 
     [background setContentMode:UIViewContentModeScaleToFill]; 
     [background setImage:[UIImage imageNamed:@"ipadLandscape.jpg"]]; 
     [self.view addSubview:background]; 
     */ 
    } 
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) && 
      isShowingLandscapeView) 
    { 
     isShowingLandscapeView = NO; 
     NSLog(@"Portrait!"); 

     /* 
     // Portrait orientation 
     txtLoginID.frame = CGRectMake(367, 254, 145, 30); 
     txtPassword.frame = CGRectMake(367, 289, 145, 30); 
     loginLabel.frame = CGRectMake(256, 257, 69, 21); 
     pwdLabel.frame = CGRectMake(256, 292, 80, 21); 
     loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     loginButton.frame = CGRectMake(439, 327, 73, 30); 

     // Set iPad portrait background 
     background.frame = CGRectMake(0, 0, 768, 1004); 
     [background setContentMode:UIViewContentModeScaleToFill]; 
     [background setImage:[UIImage imageNamed:@"ipadPortrait.jpg"]]; 
     [self.view addSubview:background]; 
     */ 
    } 
} 

爲什麼這不工作有什麼想法?我的日誌語句似乎沒有產生任何東西。

回答

2

對於滯後,也許試試這個方法,而不是:

- (void)willAnimateRotationToInterfaceOrientation: 
(UIInterfaceOrientation)toInterfaceOrientation 
duration:(NSTimeInterval)duration 
1

我認爲你可以通過重寫didRotateFromInterfaceOrientation:方法來直接從視圖控制器訂閱通知。請參閱Apple文檔here

+0

這工作得很好,但有擺脫明顯的滯後的一種方式?視圖位置的變化僅在方向改變之後發生,而不是在方向改變期間發生。任何建議? –