您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

自製 iOS 視圖切換框架:擺脫系統導航的樣式限制

編輯:iOS 開發綜合

專案裡用久了系統自帶的 UINavigationController,就會發現一個麻煩:導航列的樣式很難改。想換個背景、調整字型、加上自訂的轉場節奏,往往要跟系統的預設行為纏鬥半天。與其硬改,不如自己攢一套輕量的切換機制——這篇文章記錄的就是這樣一個做法。

老實說,好不好用這種事很主觀,一部分原因大概是「自己寫的總覺得順手」。但這套思路確實把視圖控制器的管理、快取與轉場動畫都收攏到一個基類裡,後續擴充相當直接。

設計思路

核心只有三件事:

  • 用一個字典型容器保存各個視圖控制器,以列舉值作為 key;
  • 記住「當前顯示的是哪一個」,作為切換時的基準;
  • 提供三種動作——直接切換、模擬 push、模擬 pop。

標頭檔

// BaseContentViewController.h
#import <UIKit/UIKit.h>

typedef enum enViewControllersId {
    HomeVC_Id = 0,          // 首頁
    ViewController_Id,      // 主視圖
    ProvincialCityVC_Id,    // 數據對比
    ComparisonVC_Id         // 顯示對比
} EnViewControllersId;

@interface BaseContentViewController : UIViewController

@property (strong, nonatomic) NSMutableDictionary *m_DicControllers;      // 存放視圖控制器
@property (assign, nonatomic) NSInteger m_nCurrentViewControllerID;       // 當前顯示的 ID

// 切換到指定視圖
- (void)changeViewControllerToCenter:(EnViewControllersId)aViewControllersId;
// 註冊視圖控制器
- (void)saveViewController:(UIViewController *)aUIViewController ToDic:(EnViewControllersId)aViewControllersId;
// 前推一層
- (void)pushViewcController:(EnViewControllersId)aViewControllersId;
// 返回上一層
- (void)popViewController:(EnViewControllersId)aViewControllersId;

@end

實作檔

// BaseContentViewController.m
#import "BaseContentViewController.h"

typedef enum {
    NomalType = 0,  // 直接切換
    PushType,       // 前推
    PopTYpe         // 返回
} ChangeType;

@implementation BaseContentViewController

- (id)init {
    if (self = [super init]) {
        self.m_DicControllers = [[NSMutableDictionary alloc] init];
    }
    return self;
}

// 以 ID 為 key 存進字典
- (void)saveViewController:(UIViewController *)aUIViewController
                     ToDic:(EnViewControllersId)aViewControllersId {
    NSString *key = [NSString stringWithFormat:@"%d", aViewControllersId];
    [self.m_DicControllers setObject:aUIViewController forKey:key];
}

- (UIViewController *)getViewController:(EnViewControllersId)aViewControllersId {
    NSString *key = [NSString stringWithFormat:@"%d", aViewControllersId];
    return [self.m_DicControllers objectForKey:key];
}

// 直接切換,不做動畫
- (void)changeViewControllerToCenter:(EnViewControllersId)aViewControllersId {
    UIViewController *current = [self.m_DicControllers objectForKey:
                                 [NSString stringWithFormat:@"%d", self.m_nCurrentViewControllerID]];
    UIViewController *next = [self.m_DicControllers objectForKey:
                              [NSString stringWithFormat:@"%d", aViewControllersId]];

    [current.view removeFromSuperview];
    [self.view addSubview:next.view];

    self.m_nCurrentViewControllerID = aViewControllersId;
}

// 新視圖由右側滑入,舊視圖向左退出
- (void)pushViewcController:(EnViewControllersId)aViewControllersId {
    UIViewController *current = [self.m_DicControllers objectForKey:
                                 [NSString stringWithFormat:@"%d", self.m_nCurrentViewControllerID]];
    UIViewController *next = [self.m_DicControllers objectForKey:
                              [NSString stringWithFormat:@"%d", aViewControllersId]];

    next.view.frame = CGRectMake(self.view.frame.size.width, next.view.frame.origin.y,
                                 next.view.frame.size.width, next.view.frame.size.height);
    [self.view addSubview:next.view];

    [UIView animateWithDuration:0.25f animations:^{
        next.view.frame = CGRectMake(0, next.view.frame.origin.y,
                                     next.view.frame.size.width, next.view.frame.size.height);
        current.view.frame = CGRectMake(-self.view.frame.size.width, next.view.frame.origin.y,
                                        next.view.frame.size.width, next.view.frame.size.height);
    } completion:^(BOOL finished) {
        [current.view removeFromSuperview];
    }];

    self.m_nCurrentViewControllerID = aViewControllersId;
}

// 新視圖由左側滑入,舊視圖向右退出
- (void)popViewController:(EnViewControllersId)aViewControllersId {
    UIViewController *current = [self.m_DicControllers objectForKey:
                                 [NSString stringWithFormat:@"%d", self.m_nCurrentViewControllerID]];
    UIViewController *next = [self.m_DicControllers objectForKey:
                              [NSString stringWithFormat:@"%d", aViewControllersId]];

    next.view.frame = CGRectMake(-self.view.frame.size.width, next.view.frame.origin.y,
                                 next.view.frame.size.width, next.view.frame.size.height);
    [self.view addSubview:next.view];

    [UIView animateWithDuration:0.25f animations:^{
        next.view.frame = CGRectMake(0, next.view.frame.origin.y,
                                     next.view.frame.size.width, next.view.frame.size.height);
        current.view.frame = CGRectMake(self.view.frame.size.width, next.view.frame.origin.y,
                                        next.view.frame.size.width, next.view.frame.size.height);
    } completion:^(BOOL finished) {
        [current.view removeFromSuperview];
    }];

    self.m_nCurrentViewControllerID = aViewControllersId;
}

@end

怎麼用

讓自己的容器控制器繼承 BaseContentViewController,先把要用到的子視圖控制器全部註冊進來:

[self saveViewController:homeVC ToDic:HomeVC_Id];
[self saveViewController:mainVC ToDic:ViewController_Id];

之後要切換,就只要呼叫:

// 前推
[self pushViewcController:ComparisonVC_Id];

// 返回
[self popViewController:HomeVC_Id];

因為所有子控制器都被字典持有,切換時不必反覆建立物件,也不會每次都重新拉資料——代價是記憶體佔用會隨註冊數量線性增加,頁面多的時候記得權衡。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved