ui笔记1

一。UINavigationController导航控制器

1.创建导航控制器,首先在AppDelegate上添加

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];

确定在根视图控制器上,添加导航控制器。


2.导航条上分3部分左右按钮,加中间视图,自己选择添加

//通过文字设置导航条左边按钮

    //设置当前导航条左边或者右边中间按钮必须用self.navigationItem

//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"文字" style:UIBarButtonItemStylePlain target:self action:@selector(printf:)];

    

    //通过系统设置导航条左边按钮

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(printf:)];

    

    //通过图片设置导航条右边按钮

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Fav_Filter_ALL_HL@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(imgNamed:)];

    

    //设置中间自定义视图

//    self.navigationItem.title = @"hello";

//    self.title = @"world";

    UITextField *textF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];

    textF.borderStyle = UITextBorderStyleRoundedRect;

    self.navigationItem.titleView = textF;

    

    //设置导航条右边按钮自定义

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(0, 0, 50, 30);

    [button setImage:[UIImage imageNamed:@"001@2x"] forState:UIControlStateNormal];

    button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    

    //设置导航条返回按钮

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];


//设置导航条的背景色

[self.navigationBar setBarTintColor:[UIColor orangeColor]];



3.导航控制器的跳转

a.跳转过用pushViewController

例子:self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"aaa" style:UIBarButtonItemStylePlain target:self action:@selector(push)];


-(void)push{

    SecondViewController *svc = [[SecondViewController alloc]init];//创建一个新视图

    [self.navigationController pushViewController:svc animated:nil];//用pushViewController跳转

}


b.跳转回来用[self.navigationController popViewControllerAnimated:YES];

//回到根视图控制器

//    [self.navigationController popToRootViewControllerAnimated:YES];


/回到指定视图控制器

[self.navigationController popToViewController:vc animated:YES];


//回到上一层视图控制器

[self.navigationController popViewControllerAnimated:YES];



=======================



二。UITouch


//触摸开始

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

{

    //获取坐标信息

    //获取到触摸点

    UITouch *touch = [touches anyObject];

    

    //转化为当前视图的具体坐标

    CGPoint point = [touch locationInView:self.view];

    

    NSLog(@"point == %@",NSStringFromCGPoint(point));

}


//触摸移动

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

{

    //获取坐标信息

    //获取到触摸点

    UITouch *touch = [touches anyObject];

    

    //转化为当前视图的具体坐标

    CGPoint point = [touch locationInView:self.view];

    

    NSLog(@"point == %@",NSStringFromCGPoint(point));

}


//触摸结束

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

{

    //获取坐标信息

    //获取到触摸点

    UITouch *touch = [touches anyObject];

    

    //转化为当前视图的具体坐标

    CGPoint point = [touch locationInView:self.view];

    

    NSLog(@"point == %@",NSStringFromCGPoint(point));

}


//触摸取消

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

{

    //获取坐标信息

    //获取到触摸点

    UITouch *touch = [touches anyObject];

    

    //转化为当前视图的具体坐标

    CGPoint point = [touch locationInView:self.view];

    

    NSLog(@"point == %@",NSStringFromCGPoint(point));

}



=======================


三。通知中心

1.什么是通知中心

通知是一种发送给一个或者多个观察者,用来通知其在程序中发生了某个事件的消息。通知机制的核心就是一个进程中单一实例的对象,被叫做通知中心(NSNotificationCenter)。当一个对象发布一个通知时,通知会先被发布 到通知中心。通知中心的作用相当于是交流所,作为通知的广播中心。程序中其他需要感知该事件的对象通过向通知中心注册就可以达到在事件发生时被通知中心及 时通知到得目的。


2.如何注册

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

//1.通知中心频道必须保持一致

    //2.必须先注册通知中心才能发送通知

    //3.通知中心不是UI控件,所以需要视图控制器被销毁的时候移除


    //添加通知中心

    //参数3:频道

    //参数4:形参

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:@"hello" object:nil];


3.如何发送通知

- (void)postNotificationName:(NSString *)aName object:(id)anObject;

//发送通知

// [[NSNotificationCenter defaultCenter] postNotificationName:@"hello" object:@"发送消息"];

 

    //带一个参数

    [[NSNotificationCenter defaultCenter] postNotificationName:@"hello" object:nil];

    NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithObject:@"1"forKey:@"one"];

    

    //带两个参数

    [[NSNotificationCenter defaultCenter] postNotificationName:@"hello" object:@"object消息" userInfo:dictM];


4.移除通知中心

- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;

5.通知中心与代理,Block区别

代理,Block:   一对一

通知中心:     一对多

使用通知中心会提高程序的耦合度.



=======================


四。NSUserDefaults沙盒

1.NSUserDefaults作用

NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等

应用场景:

     1.储存APP用户的账户和密码

     2.识别是否为第一次使用该APP


2.具体用法

//存储(不同数据类型不同储存方式)

    [[NSUserDefaults standardUserDefaults] setObject:@"hello" forKey:@"world"];

//    [[NSUserDefaults standardUserDefaults] setInteger:@"" forKey:@""]

//    [NSUserDefaults standardUserDefaults] setBool:<#(BOOL)#> forKey:<#(NSString *)#>

    //闪存同步数据

    [[NSUserDefaults standardUserDefaults] synchronize];

    

    //沙河家目录

    NSLog(@"home == %@",NSHomeDirectory());

    

    //读取数据

//    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"world"];

    

    //删除

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"world"];



=======================

五。UITabBarController(分栏控制器)


1.如何创建分栏控制器

//第一页v1里面

//想让第一页天生自带导航栏天赋,就在v1创建一个带导航栏的控制器,并将nav存入分栏中。

  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:v1];


  UITabBarController *tabbar = [[UITabBarController alloc] init];

    //分栏控制器装载所有视图控制器和导航控制器

    //tabBarController中最多只允许显示5个视图控制器

    //超出的系统会自动添加一个more

  v1.title = @"红色";

    v1.view.backgroundColor = [UIColor redColor];

    

    v2.title = @"蓝色";

    v2.view.backgroundColor = [UIColor blueColor];

    

    v3.title = @"绿色";

    v3.view.backgroundColor = [UIColor greenColor];

    

    v4.title = @"黄色";

    v4.view.backgroundColor = [UIColor yellowColor];

    

    v5.title = @"灰色";

    v5.view.backgroundColor = [UIColor grayColor];

    

    v6.title = @"橙色";

    v6.view.backgroundColor = [UIColor orangeColor];

    

    

    tabbar.viewControllers = @[nav,v2,v3,v4,v5,v6];

    

    //设置代理对象

    tabbar.delegate = self;

    

    //隐藏tabbar

//    tabbar.tabBar.hidden = YES;

    

    //设置选中tabbar的下标默认选中数组的第0个元素

//    tabbar.selectedIndex = 1;


————————————代理方法—————————————

    //被选中的时候

- (void)tabBarController:(UITabBarController *)tabBarController

 didSelectViewController:(UIViewController *)viewController

{

    //viewController当前点击的视图控制器

    NSLog(@"title == %@",viewController.title);

    //tabBarController.selectedIndex当前点击tabbarController的下标

    NSLog(@"index == %ld",tabBarController.selectedIndex);

}


//不能被选中

- (BOOL)tabBarController:(UITabBarController *)tabBarController

shouldSelectViewController:(UIViewController *)viewController

{

//    if ([viewController.title isEqualToString:@"蓝色"]) {

//        return NO;

//    }

    return YES;

}


//下面这三个方法主要用于监测对moreViewController中对view controlleredit操作

//即将改变视图控制器的位置

- (void)tabBarController:(UITabBarController *)tabBarController

willBeginCustomizingViewControllers:(NSArray *)viewControllers

{

    NSLog(@"即将改变视图控制器的位置");

}


//即将结束编辑状态

- (void)tabBarController:(UITabBarController *)tabBarController

willEndCustomizingViewControllers:(NSArray *)viewControllers

                 changed:(BOOL)changed

{

    NSLog(@"即将结束编辑状态");

    //change代表是否改变视图控制器的位置

    if (changed) {

        for(UIViewController *vc in viewControllers)

        {

            NSLog(@"vc.title == %@",vc.title);

        }

    }

    

}


//已经结束编辑状态

- (void)tabBarController:(UITabBarController *)tabBarController

didEndCustomizingViewControllers:(NSArray *)viewControllers

                 changed:(BOOL)changed

{

    NSLog(@"已经结束编辑状态");

}


/*——————————————自定义分栏控制器—————————————————————*/

由于系统的分栏控制器不能调整高度和更换图案,所以要自定义分栏控制器

使用原理:首先在要设置分栏的地方创建一个UIImageView,在view上添加按钮,每个按钮对应每一个分栏控制器,当选择其中一个按钮的时候,返回一个tag给分栏控制器,tag对应第几张控制器。然后将该控制器设为选择。


主要的类为:1.分栏控制器类,2图片控制器


1.分栏控制器类

  //1.隐藏系统的tabbar //系统tabbar高度49

    self.tabBar.hidden = YES;

    

    //2.自定义tabbar

self.customView = [[CustomView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, self.view.frame.size.width, 60)];

    

    //点击按钮回调视图控制器在tabbarcontroller中的位置

    //index代表图控制器tabbarcontroller中的下标

    

    //block实现里面是不能用self 因为不会对象不会被释放 内存泄露

    __weak typeof(self) safeSelf = self;

    [self.customView setBlock:^(NSInteger index) {

        safeSelf.selectedIndex = index;

    }];

   

    

    [self.view addSubview:self.customView];


    ViewController1 *v1 = [[ViewController1 alloc] init];

    v1.view.backgroundColor = [UIColor redColor];

    

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:v1];

    

    

    ViewController2 *v2 = [[ViewController2 alloc] init];

    v2.view.backgroundColor = [UIColor orangeColor];

    

    ViewController3 *v3 = [[ViewController3 alloc] init];

    v3.view.backgroundColor = [UIColor yellowColor];

    

    ViewController4 *v4 = [[ViewController4 alloc] init];

    v4.view.backgroundColor = [UIColor blueColor];

    

    self.viewControllers = @[nav,v2,v3,v4];


2.图片控制器

//设置图片的位置

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.image = [UIImage imageNamed:@"tabbar_bg@2x"];

        

        //打开交互

        self.userInteractionEnabled = YES;

        

        //创建tabbarItem按钮

        [self createButton:frame];

    }

    return self;

}


//设置图片上button的位置

- (void)createButton:(CGRect)frame

{

    for(int i = 0; i < 4; i++)

    {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(frame.size.width / 4 * i, 0, frame.size.width / 4, frame.size.height);

        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab%d_n@2x",i+1]] forState:UIControlStateNormal];

        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab%d_s@2x",i+1]] forState:UIControlStateSelected];

        if (i == 0) {

            button.selected = YES;

        }

        button.tag = 100 + i;

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

        

        [self addSubview:button];

    }

}


//按钮的点击事件,每个事件对应不同的分栏

- (void)changeView:(UIButton *)button

{

    for(int i = 0; i < 4; i++)

    {

        UIButton *btn = (UIButton *)[self viewWithTag:100 + i];

        btn.selected = NO;

    }

    

    button.selected = YES;

    

    //每个按钮的tag值对应不同视图控制器 通过tag来标记视图控制器

    self.block(button.tag - 100);

}



六。UIScorllView

使用原理和简单定义:可以将UIScorllView想象成一张大纸,然后在纸上放置图片,通过拖动或其他方式浏览,使用例子,如广告栏之类的。


使用步骤

//设置滚动内容的大小

    scrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT * 3);

    for(int i = 0; i < 3; i++)

    {

        for(int j = 0; j < 3; j++)

        {

            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * j, HEIGHT *i, WIDTH, HEIGHT)];

            view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];

            [scrollView addSubview:view];

        }

    }

    

    //是否分页滚动

    scrollView.pagingEnabled = YES;

    

    //设置内容位置 scrollview的偏移量

//    [scrollView setContentOffset:CGPointMake(WIDTH, HEIGHT)];

    

    //是否显示水平滚动条

    scrollView.showsHorizontalScrollIndicator = NO;

    

    //是否显示垂直滚动条

    scrollView.showsVerticalScrollIndicator = NO;

    

    //弹簧效果

    scrollView.bounces = YES;

    

    //设置代理对象

    scrollView.delegate = self;


[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(imgViewMove:) userInfo:scrollView repeats:YES];


- (void)imgViewMove:(NSTimer *)timer

{

    UIScrollView *view = (UIScrollView *)timer.userInfo;

    [view setContentOffset:CGPointMake(WIDTH, 0) animated:YES];

}


#pragma mark -- UIScrollViewDelegate 代理方法


//即将开始拖拽

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));

}


//视图停止时调用

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));

}


//视图动画结束时调用

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

{

    NSLog(@"视图动画结束时调用");

}


//拖动

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    //    NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));

}





//即将停止拖拽

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

{

//    NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));

}


//已经停止拖拽

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

//    NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));

}


//即将减速

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

{

//    NSLog(@"point == %@",NSStringFromCGPoint(scrollView.contentOffset));

}




//点击状态栏回到顶部

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView

{

    return YES;

}


//已经滑到顶部

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView

{

//    NSLog(@"已经滑到顶部");

}
























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值