日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

视图控制器及屏幕旋转

發布時間:2025/5/22 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 视图控制器及屏幕旋转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自定義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(@"豎屏");

? ? }

? ? ? ?

}

?

?

?

?

?

轉載于:https://www.cnblogs.com/star001/p/5211170.html

總結

以上是生活随笔為你收集整理的视图控制器及屏幕旋转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。