[ios] iOS 6.0 이상 회전 하기 (이전 버전과 비교 변경 부분)
| 첨부 '1' | 
|---|
OS를 업데이트 후 회전이 올바르게 동작하지 않는 것을 보고 당황했습니다.
관련 내용을 찾아보니 어렵지 않게 화면 회전에 대응할 수 있었습니다.
iOS 6.0 이상 및 하위 버전에서 화면 회전을 지원하기 위한 방법을 설명합니다.
iOS 6.0 부터 회전을 지원하는 delegate 메소드가 조금 변경되었습니다.
# iOS 기존 버전에서 화면 회전을 지원하는 코드.
1. window에 view 추가.
[self.window addSubview:viewController.view];
2. UIViewController에 지원할 회전 방향에 대한 delegate 구현
// 자동회전 지원하는 방향 
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || 
        interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
        interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}3. UIViewController에서 회전하기 전/후 호출되는 delegate 구현
- (void)willRotateToInterfaceOrientation:
    (UIInterfaceOrientation)toInterfaceOrientation
    duration:(NSTimeInterval)duration
{
    // 회전하기 전에 호출됩니다.
}
- (void)didRotateFromInterfaceOrientation:
    (UIInterfaceOrientation)fromInterfaceOrientation
{
   // 회전 후에 호출됩니다.
}shouldAutorotateToInterfaceOrientation의 UIInterfaceOrentation 파라미터 타입은..
다음과 같이 enum 타입 상수로 선언되어 있습니다.
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait =
        UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown =
        UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft =
        UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight =
        UIDeviceOrientationLandscapeLeft
};# iOS 6.0 이상에서는 무엇이 바뀌었을까요?
>> 위의 1번과 2번이 변경되었습니다.
1. window에 rooViewController 추가.
self.window.rootViewController = viewController;
2. UIViewController에 지원할 회전 방향에 대한 delegate 구현
//  자동회전 지원 유무 
- (BOOL) shouldAutorotate {
      return YES; 
}
// 지원하는 회전방향 
- (NSUInteger)supportedInterfaceOrientations {
    return (1 << UIInterfaceOrientationPortrait) |
        (1 << UIInterfaceOrientationLandscapeLeft) |
        (1 << UIInterfaceOrientationLandscapeRight);
}기존에 2가지 역할을 하던 메소드가 자동회전 유무와 회전하는 방향을 반환하는 메소드로 각각 분리되었습니다.
또 지원하는 회전방향의 경우, NSUInteger 반환값으로 변경되었는데 이 값은 아래와 같이 지원하는 회전방향의 비트가 셋팅된 비트 플래그 값입니다.
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait =
        (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft =
        (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight =
        (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown =
        (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape =
        (UIInterfaceOrientationMaskLandscapeLeft |
        UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll =
        (UIInterfaceOrientationMaskPortrait |
        UIInterfaceOrientationMaskLandscapeLeft |
        UIInterfaceOrientationMaskLandscapeRight |
        UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown =
        (UIInterfaceOrientationMaskPortrait |
        UIInterfaceOrientationMaskLandscapeLeft |
        UIInterfaceOrientationMaskLandscapeRight),
};1을 UIInterfaceOrentation 상수값 만큼 왼쪽으로 시프트해주면, 해당하는 방향의 비트를 셋팅할 수 있지요.
또 한가지 중요한 사항은,
커스텀 UINavigationController와 UITabBarController를 사용한 경우, (상속해서 사용하는 경우도 포함)
이 커스텀 클래스의 회전 지원 delegate 메소드에서는 topViewController의 회전지원 delegate 메소드로 위임해야 한다는 것입니다.
아래 코드를 참고
...
- (BOOL) shouldAutorotate {
    return [topViewController shouldAutorotate];
}
...# iOS 6.0 이상 및 하위 버전에서 화면 회전을 지원하는 코드
위의 설명들을 정리하여,
iOS 6.0 이상 및 하위버전에서 화면 회전을 올바르게 처리하기 위한 코드입니다.
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    // iOS6.0 이상
    NSString *currentVersion = [[UIDevice currentDevice] systemVersion];
    
    NSLog(@"%@", currentVersion);
    
    if ( [currentVersion compare:@"6.0" options:NSNumericSearch] !=
        NSOrderedAscending )
    {
        self.window.rootViewController = self.viewController;
    } else {
        // 하위버전
        [self.window addSubview:self.viewController.view];
    }
    [self.window makeKeyAndVisible];
    return YES;
}
#pragma mark - 회전지원 iOS6.0 이상
// 자동회전 지원유무
- (BOOL) shouldAutorotate
{
    return YES;
}
// 회전방향 지원유무
- (NSUInteger)supportedInterfaceOrientations
{
    return (1 << UIInterfaceOrientationPortrait) |
           (1 << UIInterfaceOrientationPortraitUpsideDown) |
           (1 << UIInterfaceOrientationLandscapeLeft) |
           (1 << UIInterfaceOrientationLandscapeRight);
}
#pragma mark - 회전지원 iOS6.0 미만 
// 회전방향 지원유무
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
            interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown
            );
}
#pragma mark - 회전 델리게이트 
// 공통
- (void)willRotateToInterfaceOrientation:
    (UIInterfaceOrientation)toInterfaceOrientation
    duration:(NSTimeInterval)duration
{
    // 회전하기 전에 호출됩니다.
}
- (void)didRotateFromInterfaceOrientation:
    (UIInterfaceOrientation)fromInterfaceOrientation
{
    // 회전 후에 호출됩니다.
}[출처] http://hiddenviewer.tistory.com/207
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 | 
|---|---|---|---|---|---|
| 853 | Develop | [unix] 로그파일 정리 쉘스크립트 | hooni | 2014.02.19 | 16230 | 
| 852 | Develop | [html] SVG(Scalable Vector Graphics) 간단 정리 | hooni | 2014.02.13 | 11397 | 
| 851 | Database | [sql] 쿼리로 문자열 검색해서 일괄 치환하기 | hooni | 2014.02.13 | 11275 | 
| 850 | Develop | [php] XE 관리자 IP대역 설정 오류 해결법 | hooni | 2014.02.10 | 11584 | 
| 849 | Develop | [c] 64bit 머신에서 inet_ntoa() 사용시 Segment fault 대처 방법법 | hooni | 2014.02.08 | 21711 | 
| 848 | Develop | [ios] NSString 간단한 정규식 사용법 | hooni | 2014.01.28 | 19673 | 
| » | Develop | [ios] iOS 6.0 이상 회전 하기 (이전 버전과 비교 변경 부분)   | hooni | 2014.01.27 | 38298 | 
| 846 | Develop | [ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동   | hooni | 2014.01.24 | 13515 | 
| 845 | Develop | [js] 주소표시줄 URL 읽어오기 (변경까지)   | hooni | 2014.01.21 | 1 | 
| 844 | System/OS | [linux] CentOS 6.x Cati 설치 (yum) | hooni | 2014.01.17 | 50058 | 
| 843 | Develop | [ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기 | hooni | 2014.01.16 | 16502 | 
| 842 | Develop | [ios] UIWebView 쿠키 유지 | hooni | 2014.01.16 | 12442 | 
 [ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동
							[ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동