appdelegate.h實現(xiàn)
@property(nonatomic, assign) BOOL allowLandscape; //允許橫豎屏yes為橫屏no為豎屏
appdelegate.m實現(xiàn)
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowLandscape == 1) {
return UIInterfaceOrientationMaskAll;
}else{
return (UIInterfaceOrientationMaskPortrait);
}
}
vc.m實現(xiàn)
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//在視圖出現(xiàn)的時候,將allowRotate改為1派诬,
AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
delegate.allowLandscape = 1;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//在視圖出現(xiàn)的時候瓣履,將allowRotate改為0泥耀,
AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowLandscape = 0;
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// do something before rotation
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//屏幕從豎屏變?yōu)闄M屏?xí)r執(zhí)行
NSLog(@"屏幕從豎屏變?yōu)闄M屏?xí)r執(zhí)行");
}else{
//屏幕從橫屏變?yōu)樨Q屏?xí)r執(zhí)行
NSLog(@"/屏幕從橫屏變?yōu)樨Q屏?xí)r執(zhí)行");
}
}
按鈕實現(xiàn)
橫屏
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.bounds = CGRectMake(0, 0, KSCREEN_HEIGHT, KSCREEN_WIDTH);
豎屏
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = CGRectMake(0, 0, KSCREEN_WIDTH, KSCREEN_HEIGHT);
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者