如何在设备定向的事件(而不是界面方向)上订阅自我? [英] How to subscribe self on the event of Device Orientation(not interface orientation)?
本文介绍了如何在设备定向的事件(而不是界面方向)上订阅自我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中我想调用一些方法在CCScene myscene在设备旋转(方向更改)的情况下。我禁用自动旋转(因为我希望它不会发生)。
问题是:我想根据我的设备方向改变场景中的重力。
我的代码:
- (void)onEnter
{
[super onEnter]
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationWillChange :) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationDidChange :) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)onExit
{
// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)notification_OrientationWillChange:(NSNotification *)n
{
orientation =(UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
}
- (void)notification_OrientationDidChange:(NSNotification *)n
{
if(orientation == UIInterfaceOrientationLandscapeLeft){
b2Vec2 gravity(0,-10);
world-> SetGravity(gravity);
}
if(orientation == UIInterfaceOrientationLandscapeRight){
b2Vec2 gravity(0,10);
world-> SetGravity(gravity);
}
}
但在这种情况下, (如果禁用,设备实际上不改变它的状态栏方向)
你能帮助我吗?
解决方案
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged :)
name:@UIDeviceOrientationDidChangeNotification
:零];
- (void)orientationChanged:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// do stuff
NSLog(@Orientation changed);
}
您必须检查设备方向而不是状态栏。
typedef枚举{
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,//垂直设备,底部的主页按钮
UIDeviceOrientationPortraitUpsideDown, //设备垂直放置,顶部的主页按钮
UIDeviceOrientationLandscapeLeft,//面向水平,主页按钮在右侧
UIDeviceOrientationLandscapeRight,//设备水平放置,左边的主页按钮
UIDeviceOrientationFaceUp ,//面向设备的平面,面向上
UIDeviceOrientationFaceDown //面向设备的平面,面朝下
} UIDeviceOrientation;
in my app i want to call some method in CCScene myscene in the case of device rotation(orientation change).I disabled the autorotation(because i want it not to happen).
The issue is: i want to change gravity in the scene depending on my device orientation. My code :
-(void) onEnter
{
[super onEnter];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void) onExit
{
//[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
//[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void)notification_OrientationWillChange:(NSNotification*)n
{
orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
}
-(void)notification_OrientationDidChange:(NSNotification*)n
{
if (orientation == UIInterfaceOrientationLandscapeLeft) {
b2Vec2 gravity( 0, -10);
world->SetGravity(gravity);
}
if (orientation == UIInterfaceOrientationLandscapeRight) {
b2Vec2 gravity( 0, 10);
world->SetGravity(gravity);
}
}
But in this case i can get notifications only in the case of autorotation enabled.(if it disabled, device is actually don't change it status bar orientation) Can you help me?
解决方案
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
- (void)orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//do stuff
NSLog(@"Orientation changed");
}
You must check for Device orientation not status bar.
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down
} UIDeviceOrientation;
这篇关于如何在设备定向的事件(而不是界面方向)上订阅自我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文