iOS에서 기기의 방향을 어떻게 감지합니까?
iOS에서 기기 방향을 감지하는 방법에 대한 질문이 있습니다. 변경 알림을받을 필요가없고 현재 방향 자체 만받습니다. 이것은 다소 간단한 질문 인 것 같지만, 나는 그것에 대해 내 머리를 감쌀 수 없었다. 다음은 지금까지 내가 한 일입니다.
UIDevice *myDevice = [UIDevice currentDevice] ;
[myDevice beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation deviceOrientation = myDevice.orientation;
BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation);
[myDevice endGeneratingDeviceOrientationNotifications];
내 마음에 이것은 작동합니다. 장치가 장치 방향 알림을 수신하도록 설정 한 다음 어떤 방향인지 묻지 만 작동하지 않고 이유를 모르겠습니다.
정말 오래된 스레드이지만 실제 솔루션은 없습니다.
나는 같은 문제가 있었지만 UIDeviceOrientation을 얻는 것이 항상 일관 적이지는 않다는 것을 알았으므로 대신 이것을 사용하십시오.
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == 0) //Default orientation
//UI is in Default (Portrait) -- this is really a just a failsafe.
else if(orientation == UIInterfaceOrientationPortrait)
//Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
// Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
//Do something if right
UIViewController :
if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
{
//
}
UIView :
if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
//
}
UIDevice.h :
#define UIDeviceOrientationIsPortrait(orientation) ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)
업데이트 :
이 코드를 xxx-Prefix.pch에 추가하면 어디서나 사용할 수 있습니다.
// check device orientation
#define dDeviceOrientation [[UIDevice currentDevice] orientation]
#define isPortrait UIDeviceOrientationIsPortrait(dDeviceOrientation)
#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
#define isFaceUp dDeviceOrientation == UIDeviceOrientationFaceUp ? YES : NO
#define isFaceDown dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO
용법:
if (isLandscape) { NSLog(@"Landscape"); }
먼저 찾고있는 내용에 대해 오리엔테이션이 변경되면 알림을 받아야합니다! viewDidLoad에서이 사물을 설정할 수 있습니다.
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
당신의 장치의 방향을 변경할 때마다 OrientationDidChange은 당신이 할 수있는 당신은 당 방향으로 원하는 호출
-(void)OrientationDidChange:(NSNotification*)notification
{
UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];
if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
{
}
else if(Orientation==UIDeviceOrientationPortrait)
{
}
}
가속도계에서 직접 장치 방향 을 얻으려면 [[UIDevice currentDevice] orientation]. 그러나 응용 프로그램의 현재 방향 ( 인터페이스 방향 )이 필요한 경우 [[UIApplication sharedApplication] statusBarOrientation].
UIViewControllerinterfaceOrientation뷰 컨트롤러의 현재 방향을 찾기 위해 액세스 할 수 있는 속성이 있습니다.
예를 들어, 작동합니다. 작동하지 않는다는 것은 무엇을 의미합니까? 예상 한 결과와 비교하여 어떤 결과를 제공합니까?
Swift 3.0에서
장치 방향을 가져옵니다.
/* return current device orientation.
This will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.
*/
UIDevice.current.orientation
to get device orientation from your app
UIApplication.shared.statusBarOrientation
Wasn't satisfied by "UIDeviceOrientation" because when a UIViewcontroller orientation is fixed to a specific orientation you don't get a pertinent information with the device orientation, so the right thing to do is using "UIInterfaceOrientation".
You can get the orientation from the UIViewController with a "self.interfaceOrientation", but when you are factorizing our code, you might need to do this kind of test outside a view controller, (custom view, a category…), so you still can access the information anywhere outside the controller by using the rootviewController:
if (UIInterfaceOrientationIsLandscape(view.window.rootViewController.interfaceOrientation)) {
}
There's a way to achieve this whether the orientation lock is enabled or not by using data from CoreMotion. This is the code:
#import <CoreMotion/CoreMotion.h>
CMMotionManager *cm=[[CMMotionManager alloc] init];
cm.deviceMotionUpdateInterval=0.2f;
[cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *data, NSError *error) {
if(fabs(data.gravity.x)>fabs(data.gravity.y)){
NSLog(@"LANSCAPE");
if(data.gravity.x>=0){
NSLog(@"LEFT");
}
else{
NSLog(@"RIGHT");
}
}
else{
NSLog(@"PORTRAIT");
if(data.gravity.y>=0){
NSLog(@"DOWN");
}
else{
NSLog(@"UP");
}
}
}];
Have you unlocked the hardware lock for device orientation? There is one at the edge of my iPad 1.
Here is some Swift variables to make detection easier:
let LANDSCAPE_RIGHT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight
let LANDSCAPE_LEFT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft
let LANDSCAPE: Bool = LANDSCAPE_LEFT || LANDSCAPE_RIGHT
let PORTRAIT_NORMAL: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait
let PORTRAIT_REVERSE: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown
let PORTRAIT: Bool = PORTRAIT_REVERSE || PORTRAIT_NORMAL
My current way of doing this:
+ (BOOL)isPortrait {
let window = UIApplication.sharedApplication.delegate.window;
if(window.rootViewController) {
let orientation =
window.rootViewController.interfaceOrientation;
return UIInterfaceOrientationIsPortrait(orientation);
} else {
let orientation =
UIApplication.sharedApplication.statusBarOrientation;
return UIInterfaceOrientationIsPortrait(orientation);
}
}
If there is for some reason no rootViewController yet fail safe to statusBarOrientation...
참고URL : https://stackoverflow.com/questions/5559652/how-do-i-detect-the-orientation-of-the-device-on-ios
'IT TIP' 카테고리의 다른 글
| 뷰를 사용하여 PHP에서 현재 Laravel 버전의 버전을 어떻게 에코 할 수 있습니까? (0) | 2020.10.27 |
|---|---|
| DATETIME 형식 값에 1 일 추가 (0) | 2020.10.27 |
| 가상 환경에서 Spyder를 실행하는 방법은 무엇입니까? (0) | 2020.10.27 |
| Android : 창을 추가 할 수 없습니다. (0) | 2020.10.27 |
| jQuery 테이블 행의 각 루프 (0) | 2020.10.27 |