在给定的事件处理程序(而不是“shouldAutorotateToInterfaceOrientation”方法),我怎样检测目前iPad的方向? 我有一个文本字段我在横向视图动画了(键盘出现时),而不是在纵向视图,并想知道我是哪个方位,看动画是必要的。
Answer 1:
朝向信息是不是很一致,有几种方法。 如果在一个视图控制器,你可以使用interfaceOrientation
属性。 从其他地方可以拨打:
[[UIDevice currentDevice] orientation]
另外,您也可以请求接收方向更改通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
还有些人喜欢以检查其状态栏方向:
[UIApplication sharedApplication].statusBarOrientation
Answer 2:
我认为
[[UIDevice currentDevice] orientation];
是不是真的可靠。 有时工作,有时不...在我的应用程序中,我使用
[[UIApplication sharedApplication]statusBarOrientation];
和它的伟大工程!
Answer 3:
之一:
- 检查
interfaceOrientation
活动视图控制器的性能。 -
[UIApplication sharedApplication].statusBarOrientation
。 -
[UIDevice currentDevice].orientation
。 (您可能需要调用-beginGeneratingDeviceOrientationNotifications
。)
Answer 4:
我发现了一个窍门来解决朝上方向的问题!
延迟方向检查出炉后的应用程序已开始运行,然后设置变量,查看大小等。!!!
//CODE
- (void)viewDidLoad {
[super viewDidLoad];
//DELAY
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(delayedCheck)
userInfo:nil
repeats:NO];
}
-(void)delayedCheck{
//DETERMINE ORIENTATION
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
FACING = @"PU";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
FACING = @"PD";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
FACING = @"LL";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
FACING = @"LR";
}
//DETERMINE ORIENTATION
//START
[self setStuff];
//START
}
-(void)setStuff{
if( FACING == @"PU" ){
//logic for Portrait
}
else
if( FACING == @"PD" ){
//logic for PortraitUpsideDown
}
else{
if( FACING == @"LL"){
//logic for LandscapeLeft
}
else
if( FACING == @"LR" ){
//logic for LandscapeRight
}
}
//CODE
你可以addSubviews,位置元素等在“setStuff”功能......任何最初将取决于方向!
:d
克里斯爱莲心
Answer 5:
您可以通过两种方式实现:
1-通过使用下面的方法:
**放入以下行-(void)viewDidLoad
的方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
然后把这个方法您的类中
-(void)deviceRotated:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your textField animation here
}
}
上述方法将检查取向时,该设备将被旋转
2-第二种方法是通过插入内部以下通知-(void)viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
然后把下面的方法你的类中
-(void)checkRotation:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your textField animation here
}
}
上述方法将检查的iPad或iPhone的状态栏的方向,并根据它,你凑合动画在需要的方向。
Answer 6:
为了确定景观VS肖像,有一个内置的功能:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
BOOL inLandscape = UIDeviceOrientationIsLandscape(orientation);
Answer 7:
[UIApplication sharedApplication].statusBarOrientation
返回人像时,它的景观,景观时,它的肖像在启动时,在iPad上
Answer 8:
我不知道为什么,但每次我的应用程序启动时,第4是正确的,但后来我得到了相反的方向。 我用一个静态变量来计算这一点,那么有一个BOOL翻转我怎么手动发送这子视图。
因此,虽然我不是增加一个新的独立的答案,我说用上面并牢记这一点。 注:我收到了状态栏的方向,因为它是被调用的唯一的事情时,应用程序启动时,是“正确的足够”,以帮我搬东西。
使用此的主要问题是被延迟加载的意见。 一定要打电话给你载和子视图的视图属性“之前”设置以满足他们的方向自己的立场。 感谢苹果不会崩溃,当我们设定不存在的变量,迫使我们要记住他们打破OO并迫使我们这样做,太... GAH,这样一个优雅的系统又是那么坏! 说真的,我很喜欢本土的,但它只是不是很好,鼓励穷人OO设计。 不是我们的错,只是提醒你调整大小功能可能会被工作,但苹果的方式要求你使用加载视图,而不是建立和初始化它
Answer 9:
在您的视图控制器,得到self.interfaceOrientation的只读值(该接口的电流方向)。
Answer 10:
我已经尝试了许多上述方法,但似乎没有什么工作,100%对我来说。
我的解决办法是让所谓的根视图控制器类型UIInterfaceOrientation的方向伊娃。
- (void)viewDidLoad {
[super viewDidLoad];
orientation = self.interfaceOrientation; // this is accurate in iOS 6 at this point but not iOS 5; iOS 5 always returns portrait on app launch through viewDidLoad and viewWillAppear no matter which technique you use.
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return YES;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
orientation = toInterfaceOrientation;
}
然后,任何地方,你需要检查你可以做这样的事情的方向:
if(UIInterfaceOrientationIsPortrait(orientation)){
// portrait
}else{
// landscape
}
有可能仍然是一个更好的办法,但是这似乎工作的时间(iOS5的仍然)98%,是不是太难。 需要注意的是iOS5的总是推出的iPad在纵向视图,然后发送设备的willRotateTo-和didRotateFromInterfaceOrientation:消息,因此该值仍然会短暂地不准确的。
Answer 11:
[UIDevice currentDevice].orientation
的伟大工程。
但!!! ...关键是将其添加到- (void)viewWillAppear:(BOOL)animated
EXP:
(void)viewWillAppear:(BOOL)animated{
...
BOOL isLandscape = UIInterfaceOrientationIsLandscape(self.interfaceOrientation);
...
}
如果你把它叫做- (void)viewDidLoad
,它不工作可靠,特别是如果你使用多线程(主UI线程,后台线程能够访问大量的外部数据,...)。
评论:1)即使您的应用程序设置默认方向纵向,用户可以在景观锁定。 因此,设置默认是不是真的解决它的解决方案。 2)有喜欢隐藏导航栏,放置在viewWillAppear中,使其工作,并在同一时间避免闪烁等任务。 同样适用于像UITableView的其他意见willDisplayCell
- >用它来设置cell.selected和cell.accessoryType。