IOS入门-页面控制

UINavigationController(导航控制器)

添加根视图

//
//  ViewController.m
//  NavigationController
#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实现跳转

//
//  FirstViewController.m
//  NavigationController
//
//  Created by clz on 2019/9/8.
//  Copyright © 2019 clz. All rights reserved.
//

#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中

//
//  ViewController.m
//  UITabBarController
//
//  Created by clz on 2019/9/8.
//  Copyright © 2019 clz. All rights reserved.
//

#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{
    //tabbarController  最大
    //navigationControlelr
    //里面放的是viewcontroller
    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进行跳转

//  ViewController.m
//  页面的跳转
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    /*
     push与present都可以推出新的界面。
     present与dismiss对应,push和pop对应。
     present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到根vc,其他vc。
     present一般用于不同业务界面的切换,push一般用于同一业务不同界面之间的切换
     */
    
}

- (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进行跳转

//  FirstViewController.m
//  页面的跳转
#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返回上一页

//  SecondViewController.m
//  页面的跳转

#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 popViewControllerAnimated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)push:(UIButton *)sender{
    ThirdViewController *third = [[ThirdViewController alloc]init];
    [self.navigationController pushViewController:third animated:YES];
}

@end

页面布局讲解

//  FirstViewController.m
//  页面布局讲解
#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添加扩展

//  ViewController.m
//  横竖屏的控制
#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实现转屏

//  ViewController.m
//  横竖屏的控制

#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

//  ViewController.m
//  UIDevice和UIScreen
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //UIDevice
    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
    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

手势

//
//  ViewController.m
//  点击手势
//
//  Created by clz on 2019/9/15.
//  Copyright © 2019 clz. All rights reserved.
//

#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.bounds = CGRectMake(0, 0, 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];
    //轻扫
//    self.view
    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值