视图控制器及屏幕旋转

自定义View

@implementation Rootview

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self)

    {

        self.button = [UIButton buttonWithType:UIButtonTypeSystem];

        self.button.frame = CGRectMake(100, 100, 100, 30);

        [self.button setTitle:@"跳转" forState:UIControlStateNormal];

        [self addSubview:_button];

        

        

        self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 30)];

        self.textField.borderStyle = UIButtonTypeRoundedRect;

        self.textField.placeholder = @"请输入";

        [self addSubview:_textField];

        [self.textField release];

    }

    return self;

 

}

// 重写layoutSubviews

// 当视图被初始化的时候 系统会自动调用此方法

// 当视图frame改变的时候 系统会自动调用此方法

// 当我们frame改变 系统会调用此方法 系统内部还会自己再调用一次

- (void)layoutSubviews

{

    [super layoutSubviews];

    // 获得的是设备的方向

    // 方向分为四种

    // 1.home建在下 2.home建在上 3.home建在左 4.home建在右

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    switch (orientation)

    {

        case UIInterfaceOrientationPortrait:

            NSLog(@"home建在下");

            break;

        case UIInterfaceOrientationPortraitUpsideDown:

            NSLog(@"home建在上");

            break;

        case UIInterfaceOrientationLandscapeLeft:

            NSLog(@"home建在左");

            break;

        case UIInterfaceOrientationLandscapeRight:

            NSLog(@"home建在右");

            break;

            

        default:

            break;

    }

 

}

 

 

 

 

 

 

// loadView 系统内部的方法中 实现了给控制器的View进行了赋值

// 此时此刻 我们进行了重写 如果不在自己重写的loadview方法中 出现给self.view = xxx 的赋值过程 那么之后我们在操作self.view的时候 相当于在操作空指针 会导致程序崩溃

// 程序的执行顺序 如果有loadView 会先走loadView方法 再走viewDidLoad的方法 然后会走视图将要出现 然后再走视图已经出现

 

- (void)loadView

{

    self.rootView = [[Rootview alloc]init];

    self.rootView.backgroundColor = [UIColor greenColor];

    self.view = _rootView;

    [_rootView release];

}

 

// viewDidLoad 视图被加载的时候 会自动调用的方法

//比较常用的是viewDidLoad

- (void)viewDidLoad {

//    [super viewDidLoad];

//    // Do any additional setup after loading the view.

//    // 每一个视图控制器 都有一个自己自带的UIView 我们把所有的空间 都需要铺放到视图控制器自带的View

//    // 每一个视图控制器 在以后我们实际开发中 相当于一个界面 一个视图控制器 对应一个界面

//    // 视图控制的主要作用:1.分担AppDelegate的工作量抽离代码 2.更好的实现了界面之间的划分

    self.view.backgroundColor = [UIColor redColor];

//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

//    button.frame = CGRectMake(100, 100, 100, 30);

//    [button setTitle:@"跳转" forState:UIControlStateNormal];

//    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

//    [self.view addSubview:button];  

    //[button release];

    

    // 创建自定义视图类的对象

    

    self.rootView = [[Rootview alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    //self.rootView.backgroundColor = [UIColor greenColor];

    [self.rootView.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_rootView];

    [_rootView release];

    self.rootView.textField.delegate = self;

    

 

}

#pragma mark ---点击return回收键盘---

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}

 

#pragma mark ---点击空白回收键盘---

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.rootView.textField resignFirstResponder];

}

#pragma mark --- button触发方法---

- (void)buttonAction:(UIButton *)button

{

    // 创建第二个界面的对象 从第一个界面跳转到第二个界面

    // 每回点击跳转 都需要重新创建一个对象 保证对象每一回都是新的

//    SecondViewController *secondViewController = [[SecondViewController alloc]init];

//    [self presentViewController:secondViewController animated:YES completion:nil];

//     

//    /*

//   [self presentViewController:secondViewController animated:YES completion:^{// 在跳转完成的同时附加的效果

//        self.view.backgroundColor = [UIColor greenColor];

//    }];

//     */

//    [secondViewController release];

    SecondViewController *secondViewController = [[SecondViewController alloc]init];

    [self presentViewController:secondViewController animated:YES completion:nil];

    [secondViewController release];

}

 

#pragma mark ---视图出现或者消失的时候会自动调用的方法---

// 视图将要出现

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    NSLog(@"第一个界面将要出现");

}

// 视图已经出现

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    NSLog(@"第一个界面已经出现");

}

// 视图将要消失

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    NSLog(@"第一个界面将要消失");

}

// 视图已经消失

- (void)viewDidDisappear:(BOOL)animated

{

    [super viewDidDisappear:animated];

    NSLog(@"第一个界面已经消失");

}

 

 

#pragma mark ---屏幕旋转-----

// 屏幕旋转需要几个步骤

// 1.支持屏幕旋转

// 默认是YES 如果是NO 不支持屏幕旋转

- (BOOL)shouldAutorotate

{

    return YES;

}

// 2.支持旋转的方向

-(UIInterfaceOrientationMask)supportedInterfaceOrientations

//- (NSUInteger )supportedInterfaceOrientations

{

    // 支持所有的方向

    // 工程设置里的方向支持 我们也要勾选上

    //return UIInterfaceOrientationMaskAll;

    return UIInterfaceOrientationMaskAllButUpsideDown;

    

}

// 已经弃用方法

// 设备将要旋转时 会自动调用的方法 iOS8之后已经弃用 强打还能够实现

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

    NSLog(@"设备将要旋转");

}

// 设备已经旋转时 会自动调用的方法 iOS8之后已经弃用 强打还能够实现

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

    NSLog(@"设备已经旋转到目标方向");

}

 

#pragma mark --- 新的屏幕旋转方法---

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

{

    if (size.width > size.height)

    {

        NSLog(@"横屏");

    }

    else

    {

        NSLog(@"竖屏");

    }

       

}

 

 

 

 

 

posted @ 2016-02-23 20:44  mingxing  阅读(187)  评论(0编辑  收藏  举报