iOS筆記-一些零散的知識點(中)

UITabBarController使用

  • UITabBarController的使用步驟

    • 初始化UITabBarController
    • 設(shè)置UIWindow的rootViewController為UITabBarController
    • 根據(jù)具體情況,通過addChildViewController方法添加對應(yīng)個數(shù)的子控制器
  • UITabBarController添加控制器的兩種方式

    • 1.添加單個子控制器
     - (void)addChildViewController:(UIViewController *)childController;
    
    • 2.設(shè)置子控制器數(shù)組
      @property(nonatomic,copy) NSArray *viewControllers;
  • UITabBarButton里面顯示什么內(nèi)容,由對應(yīng)子控制器的tabBarItem屬性決定

  • UITabBarItem有以下屬性影響著UITabBarButton的內(nèi)容

    • 標(biāo)題文字
      @property(nonatomic,copy) NSString *title;

    • 圖標(biāo)
      @property(nonatomic,retain) UIImage *image;

    • 選中時的圖標(biāo)
      @property(nonatomic,retain) UIImage *selectedImage;

    • 提醒數(shù)字
      @property(nonatomic,copy) NSString *badgeValue;

  • 示例代碼:

    • 手動在AppDelegate.m文件下手動創(chuàng)建
// 程序啟動完成的時候就會調(diào)用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 1.創(chuàng)建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 2.設(shè)置窗口的根控制器
    // 2.1 創(chuàng)建UITabBarController
    UITabBarController *tabBarVc = [[UITabBarController alloc] init];
    // 2.2 設(shè)置tabBarVc為窗口根控制器
    self.window.rootViewController = tabBarVc;

    // 3.添加子控制器
    // 3.1創(chuàng)建子控制器
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];

    // 3.2設(shè)置UITabBarButton內(nèi)容
    // 標(biāo)題
    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 = @"聯(lián)系人";
    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之后才會默認(rèn)渲染
    // 默認(rèn)UITabBar上的按鈕如果被選中了,圖片會被渲染成藍(lán)色
    // tabBarVC默認(rèn)顯示第0個控制器的view

    // 3.顯示窗口
    [self.window makeKeyAndVisible];

    return YES;
}

Modal的使用

  • 除了push之外,還有另外一種控制器的切換方式,那就是Modal

  • 任何控制器都能通過Modal的形式展示出來

  • Modal的默認(rèn)效果:新控制器從屏幕的最底部往上鉆,直到蓋住之前的控制器為止

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

  • 關(guān)閉當(dāng)初Modal出來的控制器方法:
    - (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;

  • Modal原理解剖:

    • 手寫Modal
    // 誰modal誰就強引用modal出來的控制器
    // oneVc被ViewController的presentedViewController強引用

    // 自己需要Modal出OneViewController的view
    // 1.把OneViewController的view添加到窗口上,移除之前窗口上的view

    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:oneVc.view];
    // 2.慢慢往上鉆的動畫
    // 先設(shè)置形變
      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的區(qū)別:

事件處理

  • iOS中的事件可以分為3大類型

    • 1.觸摸事件(重點掌握)
    • 2.加速器事件
    • 3.遠(yuǎn)程控制事件
  • 響應(yīng)者對象(UIResponder)

    • 在iOS中不是任何對象都能處理事件,只有繼承了UIResponder的對象才能接收并處理事件。我們稱之為“響應(yīng)者對象”
    • UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應(yīng)者對象,都能夠接收并處理事件
  • UIResponder內(nèi)部提供了以下方法來處理事件

  • 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.遠(yuǎn)程控制事件
    - (void)remoteControlReceivedWithEvent:(UIEvent *)event;

UIView的觸摸事件處理

  • UIView是UIResponder的子類,可以覆蓋下列4個方法處理不同的觸摸事件
    • 一根或者多根手指開始觸摸view,系統(tǒng)會自動調(diào)用view的下面方法
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    • 一根或者多根手指在view上移動,系統(tǒng)會自動調(diào)用view的下面方法(隨著手指的移動,會持續(xù)調(diào)用該方法)
      - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

    • 一根或者多根手指離開view,系統(tǒng)會自動調(diào)用view的下面方法
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

    • 觸摸結(jié)束前,某個系統(tǒng)事件(例如電話呼入)會打斷觸摸過程,系統(tǒng)會自動調(diào)用view的下面方法
      - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
      提示:touches中存放的都是UITouch對象

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容