/**
iOS如何指定某个页面可以旋转屏幕,其余控制器都正常竖屏?
在你要旋转的controller中一开始的地方写这两句就可以了
**/
- (void)setupView{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = 1;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = 0;
}
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (_allowRotation == 1) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}else{
return (UIInterfaceOrientationMaskPortrait);
}
}
// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
if (_allowRotation == 1) {
return YES;
}
return NO;
}
//方法二
#import "TwoViewController.h"
#import "AppDelegate.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationItem setTitle:@"方法一"];
[self.view setBackgroundColor:[UIColor lightGrayColor]];
// [self.navigationController pushViewController:[[ThreeViewController alloc]init] animated:YES];
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(80, 80, 200, 200)];
[tv setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:tv];
// [self performSelector:@selector(aa) withObject:UITextViewTextDidBeginEditingNotification afterDelay:1];
}
-(void)viewWillAppear:(BOOL)animated{
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
//是否可以旋转
-(BOOL)shouldAutorotate
{
return YES;
}
@end
//方法三
#import "ThreeViewController.h"
@interface ThreeViewController ()
@end
@implementation ThreeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationItem setTitle:@"方法二"];
[self.view setBackgroundColor:[UIColor lightGrayColor]];
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(80, 80, 200, 200)];
[tv setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:tv];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* *iOS中可以直接调用某个对象的消息方式有两种
*1.performSelector:withObject;
*2.NSInvocation
*
*/
//使用这里的代码也是oK的。 这里利用 NSInvocation 调用 对象的消息
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
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 = UIInterfaceOrientationLandscapeLeft;//横屏
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
//是否可以旋转
-(BOOL)shouldAutorotate
{
return YES;
}
#import "ViewController.h"
@interface ViewController ()
{
TwoViewController *vcTwo;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view setBackgroundColor:[UIColor whiteColor]];
vcTwo = [[TwoViewController alloc]init];
UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(50, 120, 80, 80)];
[bt setTitle:@"push1" forState:UIControlStateNormal];
[bt addTarget:self action:@selector(pushaction) forControlEvents:UIControlEventTouchUpInside];
[bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[bt setBackgroundColor:[UIColor redColor]];
[self.view addSubview:bt];
UIButton *bt2 = [[UIButton alloc]initWithFrame:CGRectMake(50, 240, 80, 80)];
[bt2 setTitle:@"push2" forState:UIControlStateNormal];
[bt2 addTarget:self action:@selector(pushaction2) forControlEvents:UIControlEventTouchUpInside];
[bt2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[bt2 setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:bt2];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)pushaction{
[self.navigationController pushViewController:vcTwo animated:YES];
}
-(void)pushaction2{
ThreeViewController *vc = [[ThreeViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
//是否可以旋转
- (BOOL)shouldAutorotate
{
return YES;
}
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end