2011-04-30 1 views

답변

2

예 가능하지만 회전 이벤트를 수동으로 처리해야합니다.

의 viewDidLoad에서

,

// store the current orientation 
currentOrientation = UIInterfaceOrientationPortrait; 
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(receivedRotate:) name: UIDeviceOrientationDidChangeNotification object: nil]; 

if(currentOrientation != self.interfaceOrientation) { 
    [self deviceInterfaceOrientationChanged:self.interfaceOrientation]; 
} 

을 추가하고 컨트롤러가 제거 될 때 이벤트를 등록 해제하는 것을 잊지 마세요. 내가 어떤 방향으로 회전을 허용하지 않습니다, 당신은 수도

- (void)deviceInterfaceOrientationChanged:(UIInterfaceOrientation)interfaceOrientation { 

    if(interfaceOrientation == currentOrientation) { 
     NSLog(@"Do not rotate to current orientation: %i", interfaceOrientation); 
    } else if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     NSLog(@"Do not rotate to UIInterfaceOrientationPortraitUpsideDown"); 
    } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     NSLog(@"Do not rotate to UIInterfaceOrientationLandscapeLeft"); 
    } else { 

     if(!isRotating) 
     { 
      isRotating = YES; 

      if(currentOrientation == UIInterfaceOrientationPortrait && interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
       NSLog(@"Rotate to landscape"); 

       // rotate to right top corner 
       [UIView beginAnimations:nil context:NULL]; 
       [UIView setAnimationDuration:0.5]; 

       // do your rotation here 

       [UIView setAnimationDelegate:self]; 
       [UIView setAnimationDidStopSelector:@selector(animationDoneShowCaption:finished:context:)]; 
       [UIView commitAnimations]; 


      } else if(currentOrientation == UIInterfaceOrientationLandscapeRight && interfaceOrientation == UIInterfaceOrientationPortrait) { 
       // etc 
      } 

      isRotating = NO; 
     } else { 
      NSLog(@"We are already rotating.."); 
     } 
    } 

    currentOrientation = interfaceOrientation; 
} 

참고

// This method is called by NSNotificationCenter when the device is rotated. 
-(void) receivedRotate: (NSNotification*) notification 
{ 
    NSLog(@"receivedRotate"); 
    UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation]; 

    if(interfaceOrientation != UIDeviceOrientationUnknown) { 
     [self deviceInterfaceOrientationChanged:interfaceOrientation]; 
    } else { 
     NSLog(@"Unknown device orientation"); 
    } 
} 

를 마지막으로 회전 방법 : 그런 다음 회전하는 방법을 추가 할 수 있습니다.

또한 구성 요소를 크기를 조정할 수 있도록 회전 할 수 있어야합니다.

편집 대신 block-based 애니메이션을 사용하고 완료 블록에 isRotation = NO를 설정하는 것이 좋습니다.