- -(void)rotation_icon:(float)n {
- UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>][self.view viewWithTagName:@"home_history"];
- UIButton *cam_btn = [self.view viewWithTagName:@"cam_btn"]; UIButton *cut_btn = [self.view viewWithTagName:@"cut_btn"]; UIButton *light_btn=[self.view viewWithTagName:@"light_btn"];
- history_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- cam_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- cut_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- light_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
- }
- - (void)orientationChanged:(NSNotification *)note { UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
- switch (o) {
- case UIDeviceOrientationPortrait: // Device oriented vertically, home button on the bottom
- [self rotation_icon:0.0];
- break;
- case UIDeviceOrientationPortraitUpsideDown: // Device oriented vertically, home button on the top
- [self rotation_icon:180.0];
- break;
- case UIDeviceOrientationLandscapeLeft: // Device oriented horizontally, home button on the right
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
- [self rotation_icon:90.0*3];
- break;
- case UIDeviceOrientationLandscapeRight: // Device oriented horizontally, home button on the left
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
- [self rotation_icon:90.0];
- break;
- default:
- break;
- }
- }
- -(void)viewWillDisappear:(BOOL)animated {
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
- UIDevice *device = [UIDevice currentDevice]; //Get the device object
- [nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device];
- }
- - (void)viewDidAppear:(BOOL)animated {
- // Do any additional setup after loading the view from its nib.
- //----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----
- UIDevice *device = [UIDevice currentDevice]; //Get the device object
- [device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app
- [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
在特別的場景下,需要針對屏幕旋轉作特殊處理。在ios系統下實現相關的功能還是比較方便的。
我下面介紹兩種方法:
1.注冊UIApplicationDidChangeStatusBarOrientationNotification通知(舉例:在一個viewcontroller類的viewdidload中注冊該通知),示例代碼如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:)name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
- (void)statusBarOrientationChange:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight) // home鍵靠右
{
//
}
if (
orientation ==UIInterfaceOrientationLandscapeLeft) // home鍵靠左
{
//
}
if (orientation == UIInterfaceOrientationPortrait)
{
//
}
if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//
}
}
注意這種方式監聽的是StatusBar也就是狀態欄的方向,所以這個是跟你的布局有關的,你的布局轉了,才會接到這個通知,而不是設備旋轉的通知。當我們關注的東西和布局相關而不是純粹設備旋轉,我們使用上面的代碼作為實現方案比較適合。
2.注冊UIDeviceOrientationDidChangeNotification通知(舉例:我們同樣在一個viewcontroller類的viewdidload中注冊該通知),示例代碼如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
- (void)orientChange:(NSNotification *)noti
{
NSDictionary* ntfDict = [noti userInfo];
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
/*
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down */
switch (orient)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
}
}
注意到這種方式里面的方向還包括朝上或者朝下,很容易看出這個完全是根據設備自身的物理方向得來的,當我們關注的只是物理朝向時,我們通常需要注冊該通知來解決問題(另外還有一個加速計的api,可以實現類似的功能,該api較底層,在上面兩個方法能夠解決問題的情況下建議不要用,使用不當性能損耗非常大)。
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
參考鏈接 https://blog.csdn.net/nadeal/article/details/79542682