iPhone进入横向模式时执行方法

Ton*_*Nam 3 iphone events delegates objective-c ipad

当iPhone进入横向或纵向模式时是否会调用一个代理?当iPhone旋转时,我需要更改样式并将对象放在不同的位置.我是否必须使用加速度计?此外,如果存在这样的委托,我必须在界面构建器中创建连接.我是客观的新手......

Jha*_*iya 7

注册以收听方向更改通知.

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];
Run Code Online (Sandbox Code Playgroud)

实现orientationChanged:方法,将在设备更改方向时调用.你可以放置代码来检查方向类型并调用你的方法.

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

删除通知dealloc.

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)

查看博客文章

对iPhone的方向做出反应