当 iPhone 进入横向或纵向模式时,是否有代理会被调用?当 iPhone 旋转时,我需要更改样式并将对象放置在不同的位置。我必须用加速度计做这个吗?此外,如果存在这样的委托(delegate),我是否必须在界面生成器中创建连接。我是 objective-c 的新手...
最佳答案
注册以收听方向更改通知。
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:
方法,当设备改变方向时调用该方法。您可以放置代码来检查方向类型并调用您的方法。
- (void)orientationChanged:(NSNotification *)note
{
NSLog(@"Orientation has changed: %d", [[note object] orientation]);
}
在 dealloc
中删除通知。
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
查看博文
关于iphone - 当 iPhone 进入横向模式时执行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6628273/