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

iOS 實作:圖片輪播與啟動引導頁

編輯:iOS 開發綜合

這次要分享兩個實用的小功能:啟動時的引導頁,以及可自動播放的圖片輪播。引導頁的靈感取自 code4app.com,我改寫了它的實作方式,並沿用其圖片分割的做法;輪播則是我以前寫的,特別加了計時器,能自動切換到下一張。

引導頁的用法

引導頁通常只在第一次啟動時出現,可以透過判斷本地檔案或某個設定欄位來決定是否顯示。以下是在 AppDelegate 中的使用方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[LKViewController alloc] initWithNibName:@"LKViewController" bundle:nil];

    NSMutableArray* dataArray = [NSMutableArray array];
    for (int i = 1; i <= 5; i++) {
        // 載入圖片的完整路徑
        [dataArray addObject:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"welcome-%d", i] ofType:@"jpg"]];
    }

    // 判斷是否為第一次進入,可用判斷檔案、判斷欄位等方法
    LKWelcomeView* welcome = [[LKWelcomeView alloc] initWithFrame:CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height - 20) andPathArray:dataArray];
    [self.window addSubview:welcome];

    welcome.endEvent = ^(){
        // 引導結束、進入主程式
        [self.window insertSubview:self.viewController.view atIndex:0];
    };

    [self.window makeKeyAndVisible];
    return YES;
}

輪播的用法

輪播的建構方式也很簡單,一樣先把圖片路徑放進陣列,再交給自訂的輪播視圖即可:

- (void)viewDidLoad
{
    NSMutableArray* pagearray = [NSMutableArray arrayWithCapacity:5];
    for (int i = 1; i <= 5; i++) {
        [pagearray addObject:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"page_%d", i] ofType:@"jpg"]];
    }

    LKPageView* page = [[LKPageView alloc] initWithPathStringArray:pagearray andFrame:CGRectMake(0, 50, 320, 200)];
    [self.view addSubview:page];

    [super viewDidLoad];
}

透過這兩個自訂視圖,你就能快速為 App 加上開場引導與自動輪播的圖片展示效果。

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