页面控制
- UINavigationController(导航控制器)
- 添加根视图
- 使用UINavigationController实现跳转
- UITabBarController(选项卡导航器)
- 将UINavigationController放到UITabController中
- 页面跳转
- 使用presenter进行跳转
- 使用dismiss销毁和push进行跳转
- 使用pop返回上一页
- 页面布局讲解
- 横竖屏控制
- 给UIDevice添加扩展
- 使用UIdevice实现转屏
- UIDevice和UIScreen
- 手势
UINavigationController(导航控制器)
添加根视图
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
FirstViewController *vc = [[FirstViewController alloc]init];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
[nc.navigationBar setTranslucent:NO];
[self presentViewController:nc animated:YES completion:^{
}];
}
@end
使用UINavigationController实现跳转
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.title = @"First";
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonSystemItemEdit target:self action:@selector(edit:)];
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
[editButton setTitle:@"自定义的编辑" forState:UIControlStateNormal];
[editButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[editButton setBackgroundColor:[UIColor yellowColor]];
[editButton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:editButton];
self.navigationItem.rightBarButtonItem = item1;
}
- (void)edit:(UIBarButtonItem *)sender{
SecondViewController *secondViewC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondViewC animated:YES];
}
@end
UITabBarController(选项卡导航器)
将UINavigationController放到UITabController中
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
FirstViewController *first = [[FirstViewController alloc]init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:first];
[nav1.navigationBar setTranslucent:NO];
nav1.title = @"First";
UIImage *nav1DefImage = [UIImage imageNamed:@"home_discovergray"];
nav1DefImage = [nav1DefImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *nav1SelectedImage = [UIImage imageNamed:@"home_discovegreen"];
nav1SelectedImage = [nav1SelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.image = nav1DefImage;
nav1.tabBarItem.selectedImage = nav1SelectedImage;
SecondViewController *second = [[SecondViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:second];
nav2.title = @"Second";
nav2.tabBarItem.image = [UIImage imageNamed:@"home_personalgray"];
nav2.tabBarItem.selectedImage = [UIImage imageNamed:@"home_personalgreen"];
ThirdViewController *third = [[ThirdViewController alloc]init];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:third];
nav3.title = @"Third";
nav3.tabBarItem.image = [UIImage imageNamed:@"home_studygray"];
nav3.tabBarItem.selectedImage = [UIImage imageNamed:@"home_studygreen"];
UITabBarController *tabBarControlelr = [[UITabBarController alloc]init];
tabBarControlelr.viewControllers = @[nav1,nav2,nav3];
tabBarControlelr.tabBar.translucent = NO;
[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
[self presentViewController:tabBarControlelr animated:YES completion:^{
}];
}
@end
页面跳转
使用presenter进行跳转
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
FirstViewController *first = [[FirstViewController alloc]init];
UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:first];
[self presentViewController:nv animated:YES completion:^{
}];
}
@end
使用dismiss销毁和push进行跳转
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.title = @"First";
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.frame = CGRectMake(0, 100, 100, 40);
[button setBackgroundColor:[UIColor whiteColor]];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setTitle:@"跳转" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
button1.frame = CGRectMake(0, 200, 100, 40);
[button1 setBackgroundColor:[UIColor whiteColor]];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
- (void)back:(UIButton *)sender{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)push:(UIButton *)sender{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
}
@end
使用pop返回上一页
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.title = @"Second";
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setTitle:@"返回" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
button1.frame = CGRectMake(0, 200, 100, 40);
[button1 setBackgroundColor:[UIColor whiteColor]];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setTitle:@"跳转" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
button2.frame = CGRectMake(0, 100, 100, 40);
[button2 setBackgroundColor:[UIColor whiteColor]];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
- (void)back:(UIButton *)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)push:(UIButton *)sender{
ThirdViewController *third = [[ThirdViewController alloc]init];
[self.navigationController pushViewController:third animated:YES];
}
@end
页面布局讲解
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
NSLog(@"宽2 = %f;高2 = %f",self.view.frame.size.width,self.view.frame.size.height);
NSLog(@"X2 = %f;Y2 = %f",self.view.frame.origin.x,self.view.frame.origin.y);
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(0,64, 100, 100);
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"宽3 = %f;高3 = %f",self.view.frame.size.width,self.view.frame.size.height);
NSLog(@"X3 = %f;Y3 = %f",self.view.frame.origin.x,self.view.frame.origin.y);
}
@end
横竖屏控制
给UIDevice添加扩展
#import "ViewController.h"
#import "UIDevice+Direction.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"旋转" forState:UIControlStateNormal];
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.frame = CGRectMake(0, 200, self.view.frame.size.width, 30);
[button setBackgroundColor:[UIColor redColor]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
- (void)action:(UIButton *)sender{
((AppDelegate *)[UIApplication sharedApplication].delegate).allowRotation = YES;
[UIDevice setNewOrientation:UIInterfaceOrientationLandscapeRight];
}
@end
使用UIdevice实现转屏
#import "ViewController.h"
#import "UIDevice+Direction.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"旋转" forState:UIControlStateNormal];
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.frame = CGRectMake(0, 200, self.view.frame.size.width, 30);
[button setBackgroundColor:[UIColor redColor]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
- (void)action:(UIButton *)sender{
((AppDelegate *)[UIApplication sharedApplication].delegate).allowRotation = YES;
[UIDevice setNewOrientation:UIInterfaceOrientationLandscapeRight];
}
@end
UIDevice和UIScreen
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIDevice *currentDevice = [UIDevice currentDevice];
NSLog(@"%@",currentDevice.name);
NSLog(@"%@",currentDevice.model);
NSLog(@"%@",currentDevice.localizedModel);
NSLog(@"%@",currentDevice.systemName);
NSLog(@"%@",currentDevice.systemVersion);
NSLog(@"%@",currentDevice.identifierForVendor);
NSLog(@"%d",currentDevice.multitaskingSupported);
NSLog(@"%ld",(long)currentDevice.userInterfaceIdiom);
UIScreen *screen =[UIScreen mainScreen];
NSLog(@"修改屏幕亮度前 = %f",screen.brightness);
screen.brightness = 1;
NSLog(@"修改屏幕亮度后 = %f",screen.brightness);
NSLog(@"屏幕宽度 = %f",screen.bounds.size.width);
NSLog(@"屏幕高度 = %f",screen.bounds.size.height);
}
@end
手势
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = [UIImage imageNamed:@"testImage"];
[self.view addSubview:imageView];
imageView.frame = CGRectMake(0, 200, 100, 100);
imageView.center = self.view.center;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tapGesture];
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:leftSwipe];
[self.view addGestureRecognizer:rightSwipe];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick:)];
longPressGesture.minimumPressDuration = 1;
[imageView addGestureRecognizer:longPressGesture];
}
- (void)tapClick:(UITapGestureRecognizer *)sender{
NSLog(@"触发");
}
- (void)swipeClick:(UISwipeGestureRecognizer *)sender{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"左滑动");
}else{
NSLog(@"右滑动");
}
- (void)longPressClick:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"触发");
}
}
@end