iOS笔记-一些零散的知识点(中)

UITabBarController使用

  • UITabBarController的使用步骤

    • 初始化UITabBarController
    • 设置UIWindow的rootViewController为UITabBarController
    • 根据具体情况,通过addChildViewController方法添加对应个数的子控制器
  • UITabBarController添加控制器的两种方式

    • 1.添加单个子控制器

      1
      - (void)addChildViewController:(UIViewController *)childController;
    • 2.设置子控制器数组
      @property(nonatomic,copy) NSArray *viewControllers;

  • UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定

  • UITabBarItem有以下属性影响着UITabBarButton的内容

    • 标题文字
      @property(nonatomic,copy) NSString *title;

    • 图标
      @property(nonatomic,retain) UIImage *image;

    • 选中时的图标
      @property(nonatomic,retain) UIImage *selectedImage;

    • 提醒数字
      @property(nonatomic,copy) NSString *badgeValue;

  • 示例代码:

    • 手动在AppDelegate.m文件下手动创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 程序启动完成的时候就会调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 2.设置窗口的根控制器
// 2.1 创建UITabBarController
UITabBarController *tabBarVc = [[UITabBarController alloc] init];
// 2.2 设置tabBarVc为窗口根控制器
self.window.rootViewController = tabBarVc;
// 3.添加子控制器
// 3.1创建子控制器
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
// 3.2设置UITabBarButton内容
// 标题
vc.tabBarItem.title = @"消息";
vc.tabBarItem.image = [UIImage imageNamed:@"tab_recent_nor"];
// 4.添加子控制器到tabBarVc
[tabBarVc addChildViewController:vc];
//========================================================
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor greenColor];
vc1.tabBarItem.title = @"联系人";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];
vc1.tabBarItem.badgeValue = @"99+";
[tabBarVc addChildViewController:vc1];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor yellowColor];
[tabBarVc addChildViewController:vc2];
vc2.tabBarItem.badgeValue = @"10";
// 在iOS7之后才会默认渲染
// 默认UITabBar上的按钮如果被选中了,图片会被渲染成蓝色
// tabBarVC默认显示第0个控制器的view
// 3.显示窗口
[self.window makeKeyAndVisible];
return YES;
}

Modal的使用

  • 除了push之外,还有另外一种控制器的切换方式,那就是Modal
  • 任何控制器都能通过Modal的形式展示出来
  • Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止

  • 以Modal的形式展示控制器方法:
    - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion

  • 关闭当初Modal出来的控制器方法:
    - (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;

  • Modal原理解剖:

    • 手写Modal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 谁modal谁就强引用modal出来的控制器
// oneVc被ViewController的presentedViewController强引用
// 自己需要Modal出OneViewController的view
// 1.把OneViewController的view添加到窗口上,移除之前窗口上的view
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow addSubview:oneVc.view];
// 2.慢慢往上钻的动画
// 先设置形变
oneVc.view.transform = CGAffineTransformMakeTranslation(0, keyWindow.bounds.size.height);
[UIView animateWithDuration:0.5 animations:^{
// 还原形变
oneVc.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
}];
  • Modal与push的区别:

事件处理

  • iOS中的事件可以分为3大类型

    • 1.触摸事件(重点掌握)
    • 2.加速器事件
    • 3.远程控制事件
  • 响应者对象(UIResponder)

    • 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”
    • UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件
  • UIResponder内部提供了以下方法来处理事件

  • 1.触摸事件
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

  • 2.加速计事件
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

  • 3.远程控制事件
    - (void)remoteControlReceivedWithEvent:(UIEvent *)event;

UIView的触摸事件处理

  • UIView是UIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件

    • 一根或者多根手指开始触摸view,系统会自动调用view的下面方法
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    • 一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

    • 一根或者多根手指离开view,系统会自动调用view的下面方法
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

    • 触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法
      - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
      提示:touches中存放的都是UITouch对象