2011-12-22 3 views
15

Xcode 사용 4.2.1 iPad iOS 5.0.1에서 새로운 "단일보기"iPad 프로젝트를 만듭니다. ,회전 후 UIView 좌표는 바뀌지 만 UIWindow는 바뀌지 않습니까?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

- (void) dumpView: (UIView *) view { 
    CGRect frame = view.frame ; 
    CGRect bounds = view.bounds ; 
    CGPoint center = view.center ; 

    NSLog(@"view [%@]:%d frame=%@ bounds=%@ center=%@" 
      , NSStringFromClass(view.class) 
      , [view hash] 
      , NSStringFromCGRect(frame) 
      , NSStringFromCGRect(bounds) 
      , NSStringFromCGPoint(center)) ; 
} 

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation { 

    NSLog(@"INViewController.didRotateFromInterfaceOrientation: %d to %d", fromInterfaceOrientation, self.interfaceOrientation) ; 
    [self dumpView: self.view] ; 
    [self dumpView: self.view.superview] ; 
} 

실행하여 장치를 회전하면 얻을 것이다 : 컨트롤러에 추가, 즉

INViewController.didRotateFromInterfaceOrientation: 2 to 4 
view [UIView] bounds={{0, 0}, {1024, 748}} center={394, 512} 
view [UIWindow] bounds={{0, 0}, {768, 1024}} center={384, 512} 

을의 UIView의는 "가로로 교환"그 좌표가 예상대로,하지만 부모 UIWindow는 여전히 또한

는 UIView의 크기는 약간 틀린 것 같다 ... 세로 모드로 제기 : y는 (20)가 0에에 있어야하는 좌표 ...

누구나 어 알고 이게 뭐야?

답변

37

UIWindow의 좌표계는 항상 세로 방향입니다. rootViewController의 뷰의 변환을 설정하여 회전을 적용합니다. 예를 들어, 단일 뷰 템플릿을 사용하여 테스트 응용 프로그램을 만들고 실행했습니다. 세로 방향 : 오른쪽 회전 후

(gdb) po [[(id)UIApp keyWindow] recursiveDescription] 
<UIWindow: 0x9626a90; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x9626b80>> 
    | <UIView: 0x96290e0; frame = (0 0; 748 1024); transform = [0, 1, -1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x96286a0>> 

:

(gdb) po [[(id)UIApp keyWindow] recursiveDescription] 
<UIWindow: 0x9626a90; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x9626b80>> 
    | <UIView: 0x96290e0; frame = (0 20; 768 1004); autoresize = W+H; layer = <CALayer: 0x96286a0>> 

왼쪽 회전 후 (90도 회전 행렬) 설정된 변환 방법

(gdb) po [[(id)UIApp keyWindow] recursiveDescription] 
<UIWindow: 0x9626a90; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x9626b80>> 
    | <UIView: 0x96290e0; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x96286a0>> 

통지서 때 장치가 회전합니다.

UIView 크기에 대한 질문 :보기의 경계는 자체 좌표 공간에 있으며 기본적으로보기의 경계의 원점은 좌표 공간의 원점 (0,0)에 있습니다. 뷰의 프레임은 부모의 좌표 공간에 있습니다. 위의 재귀 적 설명에서 예상했던 20 개를 직접 보거나 프레임을 직접 인쇄하여 볼 수 있습니다.

(gdb) p (CGRect)[[[[(id)UIApp keyWindow] subviews] lastObject] frame] 
$2 = { 
    origin = { 
    x = 0, 
    y = 20 
    }, 
    size = { 
    width = 768, 
    height = 1004 
    } 
} 
+1

의미가 있습니다. 고맙습니다. 덧붙여, gdb가 대화식으로 실제로 사용될 수 있다는 것을 보여 주신 것에 대해서도 감사드립니다 .-) – verec

+0

@rob UIView.frame의 ref 문서는 다음과 같이 말합니다 : 경고 transform 속성이 ID 변환이 아닌 경우이 속성의 값은 정의되지 않습니다. 따라서 무시해야합니다. 그것에 대해 자세히 설명해 주시겠습니까? 회전 된 뷰의 프레임에 애니메이션을 적용하여 오버레이로 슬라이드하려고한다고 가정 해 보겠습니다. – tribalvibes

+1

'transform'이 아이덴티티가 아니라면'frame'을 코드에 의존하지 않습니다. 그러나,'frame' 메소드는 항상'[self convertRect : self.bounds toView : self.superview]'를 반환하는 것으로 보이므로 디버깅 할 때 유용합니다. –

관련 문제