当iPhone进入横向模式时执行方法 [英] execute method when iPhone enters landscape mode

查看:138
本文介绍了当iPhone进入横向模式时执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当iPhone进入横向或纵向模式时,是否有一个被叫的代表?当iPhone旋转时,我需要更改样式并将对象放在不同的位置。我必须用加速度计这样做吗?此外,如果有这样的代理,我必须在接口构建器中创建连接。我是新客观的...

Is there a delegate that will get called when the iPhone enters landscape or portrait mode? I need to change the style and place objects in a different place when the iPhone get's rotated. Do I have to do this with the accelerometer? Moreover if there exist such a delegate do I have to create the connection in interface builder. I am new to objective-c...

推荐答案

注册以倾听方向更改通知。

Register to listen for the orientation change notification.

UIDevice *device = [UIDevice currentDevice];
    //Tell it to start monitoring the accelerometer for orientation             
[device beginGeneratingDeviceOrientationNotifications];
    //Get the notification centre for the app   
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    
[nc addObserver:self selector:@selector(orientationChanged:)        name:UIDeviceOrientationDidChangeNotification
         object:device];

实现 orientationChanged:方法,当设备改变方向时调用。您可以使用代码来检查方向类型并调用方法。

Implement orientationChanged: method, which will be called when the device change the orientation. you could put code to check the orientation type and called your method.

- (void)orientationChanged:(NSNotification *)note
{
    NSLog(@"Orientation  has changed: %d", [[note object] orientation]);
}

删除 dealloc中的通知

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

查看博文

反应iPhone的方向

这篇关于当iPhone进入横向模式时执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆