- IOS设备中的加速计可以确定设备的当前方向。默认情况,一个应用程序支持纵向和横向。当设备方向改变的时候,系统会发送UIDeviceOrientationDidChangeNotification通知,默认情况下UIKit框架监听这个通知,并自动更新这个方向。
- 四个方向如下图
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
#pragma mark - Rotation
-(void) deviceOrientation:(NSNotification *)notification
{
// NSLog(@"notification :%@",[notification userInfo]);
// NSLog(@"notification :%@",[notification object]);
UIDevice *device=(UIdevice *)[[notification object]];
NSLog(@"device :%d",device.orientation);
}
也可以这样:
#pragma mark - Rotation
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"duration :%f",duration);
UIView *button =[self.view viewWithTag:101];
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
button.frame=CGRectMake(320/2-140/2, 80, 140, 40);
} else {
button.frame=CGRectMake(480/2-140/2, 80, 140, 40);
}
}