아이폰 방향을 설정하는 문서화된 방법이 있습니까?
특정 보기에서 장치 회전을 지원하는 앱이 있지만 가로 모드에서는 특별히 의미가 없으므로 보기를 스왑할 때 회전을 강제로 세로로 설정하고 싶습니다.
UIDevice에 문서화되지 않은 속성 설정기가 있으며 이는 트릭을 수행하지만 컴파일러 경고를 생성하며 SDK의 향후 버전과 함께 사라질 수 있습니다.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
오리엔테이션을 강제하는 문서화된 방법이 있습니까?
업데이트: 이미 구현한 것처럼 인터페이스 방향 자동화를 원하는 것이 아니기 때문에 예제를 제공해야 한다고 생각했습니다.
나는 내 앱이 뷰 1에서는 풍경화와 초상화를 지원하고 뷰 2에서는 초상화만 지원하기를 원합니다.이미 모든 뷰에 대해 AutorotateToInterfaceOrientation을 구현했지만 사용자가 View 1에서 가로 모드로 전환한 후 View 2로 전환하는 경우 전화기를 다시 세로로 회전시키고 싶습니다.
이는 사실로부터 한참 후의 일이지만, 내비게이션 컨트롤러를 사용하지 않거나 문서화되지 않은 방법을 사용하지 않으려는 사람이 올 경우를 대비해 다음과 같이 설명합니다.
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
바닐라 뷰 컨트롤러를 표시하거나 해제하면 충분합니다.
분명히 당신은 여전히 autorotateToInterfaceOrientation의 재정의에서 방향을 확인하거나 거부해야 할 것입니다.하지만 이것은 자동 회전이...시스템에 의해 다시 호출됩니다.
세로에서 가로로 강제로 회전시키려면 여기에 코드가 있습니다.보기의 중심을 조정해야 합니다.저는 제 것이 경치를 제대로 배치하지 않았다는 것을 알아차렸습니다.그렇지 않으면 완벽하게 작동했습니다.알려줘서 고마워요.
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
self.view.center = CGPointMake(160.0f, 240.0f);
[UIView commitAnimations];
}
가 봤을 가제볼때,때.setOrientation:
메서드가 작동하지 않습니다(또는 더 이상 작동하지 않을 수도 있습니다).이를 위해 제가 하고 있는 일은 다음과 같습니다.
먼저 파일의 맨 위에 다음과 같은 정의를 입력합니다.
#define degreesToRadian(x) (M_PI * (x) / 180.0)
에, viewWillAppear:
방법
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
애니메이션화를 원할 경우 다음과 같이 전체를 애니메이션 블록으로 감쌀 수 있습니다.
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
[UIView commitAnimations];
그런 다음 세로 모드 컨트롤러에서 역방향으로 - 현재 가로로 되어 있는지 확인하고, 가로로 되어 있으면 다시 세로로 돌릴 수 있습니다.
고민하고 있었습니다.UIViewController
화에서에, 으로.UINavigationController
뷰 위한 가 필요했습니다.그러나 흐름에서 다음 뷰 컨트롤러를 누르면 세로 방향으로 돌아가기 위한 장치가 필요했습니다.
제가 주목한 것은,shouldAutorotateToInterfaceOrientation:
메서드는 새 뷰 컨트롤러가 스택에 푸시될 때 호출되지 않지만 뷰 컨트롤러가 스택에서 팝업될 때 호출됩니다.
이를 활용하여 다음 코드 조각을 앱 중 하나에 사용하고 있습니다.
- (void)selectHostingAtIndex:(int)hostingIndex {
self.transitioning = YES;
UIViewController *garbageController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:garbageController animated:NO];
[self.navigationController popViewControllerAnimated:NO];
BBHostingController *hostingController = [[BBHostingController alloc] init];
hostingController.hosting = [self.hostings objectAtIndex:hostingIndex];
[self.navigationController pushViewController:hostingController animated:YES];
[hostingController release];
self.transitioning = NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (self.transitioning)
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
else
return YES;
}
기본적으로 빈 뷰 컨트롤러를 만들어 스택에 밀어넣고 바로 꺼냄으로써 인터페이스를 세로 위치로 되돌릴 수 있습니다.일단 컨트롤러가 터지면, 저는 처음에 누르려던 컨트롤러를 누르기만 하면 됩니다.시각적으로 보기에는 훌륭합니다. 비어 있는 임의의 보기 컨트롤러는 사용자가 볼 수 없습니다.
kidbdallas가 이미 제공한 답변 중 두 가지를 사용하여 iPhone을 필요한 방향으로 프로그래밍할 수 있는 간단한 방법이 있습니다.
//will rotate status bar
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
//will re-rotate view according to statusbar
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
매력적으로 작동합니다 :)
편집:
iOS 6의 경우 이 기능을 추가해야 합니다. (모달 뷰 컨트롤러에서 작동)
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
}
저는 이것에 대한 좋은 해결책을 찾기 위해 계속 파고들었습니다. 이 .UIWindow
그리고 다시 추가하면 시스템이 다시 시작됩니다.shouldAutorotateToInterfaceOrientation:
올바른 방향을 적용하여 뷰 컨트롤러의 메서드를 사용할 수 있습니다.보기: iphone은 uiview의 방향을 바꾸도록 강요합니다.
이것은 더 이상 최신 iPhone 3.1.2 SDK에서 문제가 되지 않습니다.이제 스택으로 밀어넣는 보기의 요청된 방향을 따르는 것으로 나타납니다.따라서 이전 iPhone OS 버전을 검색하고 최신 버전보다 이전 버전일 때만 설정된 방향을 적용해야 합니다.
Apple의 정적 분석이 사용자가 이전 SDK 제한 사항을 해결하고 있다는 것을 이해할 수 있을지는 확실하지 않습니다.저는 개인적으로 애플로부터 다음 업데이트 때 메소드 호출을 제거하라는 지시를 받았기 때문에 오래된 장치에 대한 해킹이 승인 과정을 통과할 수 있을지는 아직 확신할 수 없습니다.
조쉬의 대답은 저에게 잘 맞습니다.
그러나 "방향이 변경되었습니다, UI를 업데이트하십시오" 알림을 게시하는 것을 선호합니다.이 알림이 뷰 컨트롤러에 의해 수신되면 호출됩니다.shouldAutorotateToInterfaceOrientation:
되돌아가서 원하는 방향을 설정할 수 있습니다.YES
당신이 원하는 방향으로.
[[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceOrientationDidChangeNotification object:nil];
유일한 문제는 이것이 애니메이션 없이 방향 전환을 강요한다는 것입니다.이 줄을 다음 사이에 묶어야 합니다.beginAnimations:
그리고.commitAnimations
원활한 전환을 달성하기 위해.
도움이 되길 바랍니다.
FWIW, 다음은 수동으로 방향을 설정하는 구현입니다(앱의 루트 보기 컨트롤러에 들어가려면).
-(void)rotateInterfaceToOrientation:(UIDeviceOrientation)orientation{
CGRect bounds = [[ UIScreen mainScreen ] bounds ];
CGAffineTransform t;
CGFloat r = 0;
switch ( orientation ) {
case UIDeviceOrientationLandscapeRight:
r = -(M_PI / 2);
break;
case UIDeviceOrientationLandscapeLeft:
r = M_PI / 2;
break;
}
if( r != 0 ){
CGSize sz = bounds.size;
bounds.size.width = sz.height;
bounds.size.height = sz.width;
}
t = CGAffineTransformMakeRotation( r );
UIApplication *application = [ UIApplication sharedApplication ];
[ UIView beginAnimations:@"InterfaceOrientation" context: nil ];
[ UIView setAnimationDuration: [ application statusBarOrientationAnimationDuration ] ];
self.view.transform = t;
self.view.bounds = bounds;
[ UIView commitAnimations ];
[ application setStatusBarOrientation: orientation animated: YES ];
}
다음과 같은 것들과 함께.UINavigationControllerDelegate
메소드(사용 중인 경우)UINavigationController
):
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
// rotate interface, if we need to
UIDeviceOrientation orientation = [[ UIDevice currentDevice ] orientation ];
BOOL bViewControllerDoesSupportCurrentOrientation = [ viewController shouldAutorotateToInterfaceOrientation: orientation ];
if( !bViewControllerDoesSupportCurrentOrientation ){
[ self rotateInterfaceToOrientation: UIDeviceOrientationPortrait ];
}
}
수신 여부에 따라 루트 뷰 회전을 처리합니다.UIViewController
는 현재 장치 방향을 지원합니다.마지막으로, 당신은 연결하고 싶을 것입니다.rotateInterfaceToOrientation
표준 iOS 기능을 모방하기 위해 실제 장치 방향 변경.이 이벤트 처리기를 동일한 루트 보기 컨트롤러에 추가합니다.
-(void)onUIDeviceOrientationDidChangeNotification:(NSNotification*)notification{
UIViewController *tvc = self.rootNavigationController.topViewController;
UIDeviceOrientation orientation = [[ UIDevice currentDevice ] orientation ];
// only switch if we need to (seem to get multiple notifications on device)
if( orientation != [[ UIApplication sharedApplication ] statusBarOrientation ] ){
if( [ tvc shouldAutorotateToInterfaceOrientation: orientation ] ){
[ self rotateInterfaceToOrientation: orientation ];
}
}
}
마지막으로 에 등록합니다.UIDeviceOrientationDidChangeNotification
의 통지.init
또는loadview
이와 같이:
[[ NSNotificationCenter defaultCenter ] addObserver: self
selector: @selector(onUIDeviceOrientationDidChangeNotification:)
name: UIDeviceOrientationDidChangeNotification
object: nil ];
[[ UIDevice currentDevice ] beginGeneratingDeviceOrientationNotifications ];
이것은 저에게 도움이 됩니다(Henry Cook 감사합니다).
저의 목적은 오직 풍경의 방향 변화만을 다루는 것이었습니다.
init 메서드:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
- (void)orientationChanged:(NSNotification *)notification {
//[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
CGRect bounds = [[ UIScreen mainScreen ] bounds ];
CGAffineTransform t;
CGFloat r = 0;
switch ( orientation ) {
case UIDeviceOrientationLandscapeRight:
r = 0;
NSLog(@"Right");
break;
case UIDeviceOrientationLandscapeLeft:
r = M_PI;
NSLog(@"Left");
break;
default:return;
}
t = CGAffineTransformMakeRotation( r );
UIApplication *application = [ UIApplication sharedApplication ];
[ UIView beginAnimations:@"InterfaceOrientation" context: nil ];
[ UIView setAnimationDuration: [ application statusBarOrientationAnimationDuration ] ];
self.view.transform = t;
self.view.bounds = bounds;
[ UIView commitAnimations ];
[ application setStatusBarOrientation: orientation animated: YES ];
}
특정 보기에서 장치 회전을 지원하는 앱이 있지만 가로 모드에서는 특별히 의미가 없으므로 보기를 스왑할 때 회전을 강제로 세로로 설정하고 싶습니다.
이 스레드의 위 원본 게시물이 현재 매우 오래되었지만 비슷한 문제가 있었습니다. 즉, 사용자가 가로와 세로 사이에서 회전할 수 있는 하나의 화면을 제외하고는 앱의 모든 화면이 세로로만 되어 있습니다.
이것은 충분히 간단했지만 다른 게시물과 마찬가지로 앱이 이전 화면으로 돌아갈 때 현재 장치 방향에 관계없이 자동으로 세로로 돌아가기를 원했습니다.
제가 구현한 솔루션은 가로 모드일 때 탐색 막대를 숨기는 것이었습니다. 즉, 사용자는 세로 모드일 때만 이전 화면으로 돌아갈 수 있습니다.따라서 다른 모든 화면은 세로로만 표시될 수 있습니다.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)pInterfaceOrientation {
BOOL lHideNavBar = self.interfaceOrientation == UIInterfaceOrientationPortrait ? NO : YES;
[self.navigationController setNavigationBarHidden:lHideNavBar animated:YES];
}
이것은 또한 가로 모드에서 사용할 수 있는 화면 공간이 더 많다는 점에서 내 앱의 추가적인 이점을 가지고 있습니다.문제의 화면은 PDF 파일을 표시하는 데 사용되기 때문에 유용합니다.
이게 도움이 되길 바랍니다.
저는 결국 이것을 꽤 쉽게 풀었습니다.위의 모든 제안을 시도했지만 여전히 부족했기 때문에 이것이 제 해결책이었습니다.
가로(왼쪽 또는 오른쪽)를 유지해야 하는 View 컨트롤러에서 방향 변경을 수신합니다.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification object:nil];
그러면 회전:
- (void) didRotate:(NSNotification *)notification
{ if (orientationa == UIDeviceOrientationPortrait)
{
if (hasRotated == NO)
{
NSLog(@"Rotating to portait");
hasRotated = YES;
[UIView beginAnimations: @"" context:nil];
[UIView setAnimationDuration: 0];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-90));
self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
self.view.frame = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
[UIView commitAnimations];
}
}
else if (UIDeviceOrientationIsLandscape( orientationa))
{
if (hasRotated)
{
NSLog(@"Rotating to lands");
hasRotated = NO;
[UIView beginAnimations: @"" context:nil];
[UIView setAnimationDuration: 0];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(0));
self.view.bounds = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
self.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
[UIView commitAnimations];
}
}
자동 크기 조정을 뷰로 사용하는 모든 수퍼 뷰/서브 뷰를 염두에 둡니다.범위/프레임이 명시적으로 재설정되고 있습니다...
가로 보기를 유지할 때 이 방법에서 유일하게 주의해야 할 점은 방향을 변경하지 않는 것이 좋을 때 발생해야 하는 고유한 애니메이션 전환입니다.
iOS 6 솔루션:
[[[self window] rootViewController] presentViewController:[[UIViewController alloc] init] animated:NO completion:^{
[[[self window] rootViewController] dismissViewControllerAnimated:NO completion:nil];
}];
정확한 코드는 앱별로 다르며 사용자가 배치하는 위치에 따라 다릅니다(AppDelegate에서 사용했습니다.를 바꿉니다.[[self window] rootViewController]
당신이 사용하는 것으로.제가 사용한 것은UITabBarController
.
저는 해결책을 찾아 프랑스어로 뭔가를 썼습니다(그러나 코드는 영어로 되어 있습니다).
방법은 창 보기에 컨트롤러를 추가하는 것입니다(컨트롤러는 shouldRotate...의 좋은 구현을 가지고 있어야 합니다).함수)를 선택합니다.
UIViewController를 사용하는 경우 다음 방법이 있습니다.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
아다가NO
회전하지 않을 뷰가 포함된 뷰 컨트롤러의 경우.
물론 UI에 90도 변환을 적용할 수 있지만 런타임에 이 작업을 수행하는 것은 불가능하다고 생각합니다.
이것이 제가 사용하는 것입니다. (컴파일 경고가 몇 개 나오지만 시뮬레이터와 아이폰 모두에서 작동합니다.)
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
언급URL : https://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation
'sourcecode' 카테고리의 다른 글
스프링 JSF 통합: 스프링 구성 요소/서비스를 JSF 관리 빈에 주입하는 방법은 무엇입니까? (0) | 2023.08.17 |
---|---|
Google Chrome devtools의 교차 스타일 속성은 무엇을 의미합니까? (0) | 2023.08.17 |
MacOS에서 도커를 쉽게 설치 및 제거하는 방법 (0) | 2023.08.17 |
Invoke-WebRequest (0) | 2023.08.17 |
jquery에서 하위 문자열을 지정하는 방법 (0) | 2023.08.17 |