2014-12-15 5 views
1

인터페이스 방향이 변경되는 경우에도 UIWindow의 맨 위에 고정하고자하는 사용자 정의 UIView이 있습니다. 여기iOS 7의 UIWindow 위에 UIView를 고정하는 방법

enter image description here

문제는 이전 아이폰 OS 8 UIWindow이 방향 변경과 변경되지 않는 좌표계이다 세로 방향으로 내보기입니다. 그래서 모든 계산을 손으로해야합니다. 우선 내가 동일하게 유지 시스템을 조정 UIWindow하는 두 번째 것은 어떻게 든 실제 좌표계를 매핑하는 것이다이 방법

-(CGFloat)angleForOrientation:(UIInterfaceOrientation)orientation 
{ 
    CGFloat angle; 
    switch (orientation) { 
     case UIInterfaceOrientationLandscapeLeft: 
      angle = -M_PI /2.0; 
      NSLog(@"UIInterfaceOrientationLandscapeLeft"); 
      break; 

     case UIInterfaceOrientationLandscapeRight: 
      angle = M_PI /2.0; 
      NSLog(@"UIInterfaceOrientationLandscapeRight"); 
      break; 

     case UIInterfaceOrientationPortraitUpsideDown: 
      angle = M_PI; 
      NSLog(@"UIInterfaceOrientationPortraitUpsideDown"); 
      break; 

     default: 
      angle = 0; 
      NSLog(@"UIInterfaceOrientationPortrait"); 
      break; 

    } 
    return angle; 
} 

를 사용하여 않는의 UIView의 변환을 변경하는 것입니다. 그러면 사용자가 다른 방향으로보기를 회전해도 화면의 가운데 중앙에 고정 된 동일한 크기의 UIView를 갖게되는 사용자 정의 프레임 UIView을 어떻게 계산해야합니까? 는 예를 들어이 뷰가이 이미지는 아이폰 OS 8 버전에서 생성되는 그런데 풍경

enter image description here

에 같이하는 방법입니다. 어떤 작업을 수행합니까?

self.frame = CGRectMake(window.bounds.size/2-50, 0, 100, 100); 
    CGFloat angle = [self angleForOrientation:orientation]; 
    self.transform = CGAffineTransformMakeRotation(angle); 

iOS 7과 비슷한 작업을 수행해야합니다. 어떻게해야합니까?

도움 주셔서 감사합니다.

+0

이 프로젝트에서 그 코드 https://github.com/hfossli/AGWindowView – Andrea

+0

감사 안드레아 살펴보십시오합니다 (변환을 처리하는 방법을보고 내 질문 참조). 그것이 내가 시도한 첫 번째 것이었지만 그것은 나의 경우를 해결하지 못했습니다. 소스 코드를 수정해야 할 수도 있습니다. – kyurkchyan

답변

1

그래서 마지막으로 이것을 구현하는 방법을 알아 냈습니다.

주어진 경계를 기준으로 뷰를 맨 위에 고정시키는 rect를 계산하기 위해 다음 메소드를 생성했습니다.

-(CGRect)getTopRectForBounds:(CGRect)bounds orientation:(UIInterfaceOrientation)orientation window:(UIWindow *)window 
{ 
    CGRect newRect; 
    CGFloat statusBarHeight = [self getStatusBarHeight]; 
    CGSize screenSize = window.screen.bounds.size; 
    CGFloat sW = screenSize.width; 
    CGFloat sH = screenSize.height; 
    CGFloat W = rect.size.width; 
    CGFloat H = rect.size.height; 
    switch (orientation) { 
     case UIInterfaceOrientationLandscapeLeft: 
      newRect = CGRectMake(statusBarHeight, (sH-W)/2, H,W); 
      break; 
     case UIInterfaceOrientationLandscapeRight: 
      newRect = CGRectMake(sW-H-statusBarHeight, (sH-W)/2, H,W); 
      break; 
     case UIInterfaceOrientationPortraitUpsideDown: 
      newRect = CGRectMake((sW-W)/2, sH-H-statusBarHeight, W,H); 
      break; 
     default: 
      newRect = CGRectMake((sW-W)/2, statusBarHeight, W,H); 
      break; 
    } 
    return newRect; 
} 

그런 다음 방향이 변경 될 때마다 프레임을 변경합니다. 방향이 변환과 프레임 방향 변경 이벤트 처리기에서 다음

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; 

을 변경하고 변경에 그래서 먼저 내가들을 수 있습니다.

CGRect bounds = CGRectMake(0, 0, 100, 100); 
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
self.frame = [self getTopRectForBounds:bounds orientation:orientation window:self.window] 
CGFloat angle = [self angleForOrientation:orientation]; 
self.transform = CGAffineTransformMakeRotation(angle);