iOS監聽裝置方向的方法
阿新 • • 發佈:2019-01-24
1.新增監聽函式
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
2.得到裝置物件
-(void)deviceOrientationDidChange:(NSObject*)sender{
UIDevice* device = [sender valueForKey:@"object"];
NSLog(@"%d",device.orientation);
}
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
2.得到裝置物件
-(void)deviceOrientationDidChange:(NSObject*)sender{
UIDevice* device = [sender valueForKey:@"object"];
NSLog(@"%d",device.orientation);
}