Gol*_*Joe 53 iphone objective-c orientation ios gyroscope
我有一个游戏,其中设备的方向影响游戏的状态.用户必须在横向,纵向和反向横向方向之间快速切换.到目前为止,我一直在通过以下方式注册游戏以获取方向通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)
但它太慢了 - 旋转电话和通知实际被解雇之间似乎有大约第二个延迟.我需要一种方法来立即检测设备方向的变化.我已经尝试过使用陀螺仪进行试验,但我还不熟悉它,以了解它是否是我正在寻找的解决方案.
Vim*_*lan 143
在viewWillAppear函数中添加通知程序
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
Run Code Online (Sandbox Code Playgroud)
方向改变通知此功能
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
Run Code Online (Sandbox Code Playgroud)
进而调用此函数处理moviePlayerController框架的方向
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
switch (orientation)
{
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
{
//load the portrait view
}
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
{
//load the landscape view
}
break;
case UIInterfaceOrientationUnknown:break;
}
}
Run Code Online (Sandbox Code Playgroud)
在viewDidDisappear删除通知
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
Run Code Online (Sandbox Code Playgroud)
我想这是你可以根据方向更改视图的最快速度
Rok*_*arc 23
这延迟你所谈论的实际上是一个过滤器,以防止错误的(不必要的)方向变化的通知.
要立即识别设备方向变化,您只需要自己监控加速度计.
加速度计测量所有3个轴的加速度(包括重力),因此在确定实际方向时不应该有任何问题.
可以在此处找到一些开始使用加速度计的代码:
这个不错的博客涵盖了数学部分:
Art*_*hur 21
为什么你没用
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Run Code Online (Sandbox Code Playgroud)
?
或者你可以使用它
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Run Code Online (Sandbox Code Playgroud)
或这个
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Run Code Online (Sandbox Code Playgroud)
希望它有用)
B.S*_*.S. 12
对于我的案例处理UIDeviceOrientationDidChangeNotification不是一个好的解决方案,因为它被称为更频繁,UIDeviceOrientation并不总是等于UIInterfaceOrientation因为(FaceDown,FaceUp).
我用它来处理它UIApplicationDidChangeStatusBarOrientationNotification:
//To add the notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation:)
//to remove the
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
...
- (void)didChangeOrientation:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
NSLog(@"Landscape");
}
else {
NSLog(@"Portrait");
}
}
Run Code Online (Sandbox Code Playgroud)
@vimal答案没有为我提供解决方案.看起来方向不是当前的方向,而是来自之前的方向.为了解决它,我使用[[UIDevice currentDevice] orientation]
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]];
}
Run Code Online (Sandbox Code Playgroud)
然后
- (void) adjustViewsForOrientation:(UIDeviceOrientation) orientation { ... }
Run Code Online (Sandbox Code Playgroud)
使用此代码,我获得当前的方向位置.
| 归档时间: |
|
| 查看次数: |
99408 次 |
| 最近记录: |