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

标签 iphone objective-c ipad events delegates

当 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];

查看博文

Reacting to iPhone's orientation

关于iphone - 当 iPhone 进入横向模式时执行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6628273/

相关文章:

iOS 滑动以从 UITableView 中删除 plist 中的数组

objective-c - cocoa 图片库窗口

javascript - &#x00A0 并且无法在 iPad 上运行

ios - GL图纸 View 在旋转时清除图纸

iphone - 创建单个脂肪文件时遇到 lipo 错误

objective-c - 不明白Apple的takeFloatValueFrom : Example

iphone - iOS 为 HTML5 Canvas 的高度宽度设置了哪些最大值?

ios - iCarousel - 动态更改中心项目 iOS

javascript - 基于屏幕尺寸的可变图像宽度/高度

html - CSS 在某些 iPhone 上异常崩溃