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

iOS 開發實戰(九):新版本特性引導頁與分享按鈕

編輯:iOS 開發綜合

一、先搞懂 iOS 的圖片加載規則

在動手寫引導頁之前,必須先弄清楚系統是怎麼挑圖片的。早期 iOS 的命名規則並不直觀,尤其 4 吋螢幕的啟動圖,需要特別留意。

非 Retina 螢幕

3.5 吋(320 x 480)使用 home.png。

Retina 螢幕

3.5 吋(640 x 960)對應 home@2x.png;4.0 吋(640 x 1136)對應 home-568h@2x.png,但這個名字只有在「home 是啟動圖」時才會被系統自動挑中。

實際對照

當 home 是啟動圖時:iPhone 1/3G/3GS 載入 home.png,iPhone 4/4S 載入 home@2x.png,iPhone 5/5S/5C 載入 home-568h@2x.png。

當 home 只是普通素材圖時,iPhone 5 系列會退回使用 home@2x.png,也就是說 -568h 這組圖這時不會被自動加載,必須自己動手。

一句話總結:home.png 給 3.5 吋非 Retina,home@2x.png 給所有 Retina,home-568h@2x.png 專屬於「4.0 吋 Retina + 啟動圖」的組合。

二、引導頁的骨架

整個頁面由一個 UIScrollView 加一個 UIPageControl 組成。滾動視圖負責橫向翻頁,頁碼控件負責回報目前位置。

#define YYNewfeatureImageCount 4

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupScrollView];
    [self setupPageControl];
}

- (void)setupScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];

    CGFloat imageW = scrollView.width;
    CGFloat imageH = scrollView.height;

    for (int i = 0; i < YYNewfeatureImageCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        if (FourInch) { // 4 吋螢幕要手動補上 -568h
            name = [name stringByAppendingString:@"-568h"];
        }
        imageView.image = [UIImage imageWithName:name];
        [scrollView addSubview:imageView];

        imageView.y = 0;
        imageView.width = imageW;
        imageView.height = imageH;
        imageView.x = i * imageW;

        if (i == YYNewfeatureImageCount - 1) {
            [self setupLastImageView:imageView];
        }
    }

    scrollView.contentSize = CGSizeMake(YYNewfeatureImageCount * imageW, 0);
    scrollView.backgroundColor = YYColor(246, 246, 246);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
}

- (void)setupPageControl {
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = YYNewfeatureImageCount;
    pageControl.centerX = self.view.width * 0.5;
    pageControl.centerY = self.view.height - 30;
    pageControl.currentPageIndicatorTintColor = YYColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = YYColor(189, 189, 189);
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

三、最後一頁放按鈕

「開始微博」與「分享給大家」兩個按鈕都加在最後一張圖片上,因此那張 UIImageView 必須先打開互動屬性。

- (void)setupLastImageView:(UIImageView *)imageView {
    imageView.userInteractionEnabled = YES;
    [self setupStarButton:imageView];
    [self setupShareButton:imageView];
}

- (void)setupStarButton:(UIImageView *)imageView {
    UIButton *startButton = [[UIButton alloc] init];
    [imageView addSubview:startButton];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"]
                          forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"]
                          forState:UIControlStateHighlighted];
    startButton.size = startButton.currentBackgroundImage.size;
    startButton.centerX = self.view.width * 0.5;
    startButton.centerY = self.view.height * 0.8; // 用比例定位,同時吃 3.5 吋與 4.0 吋
    [startButton setTitle:@"開始微博" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
}

- (void)start {
    YYTabBarViewController *vc = [[YYTabBarViewController alloc] init];
    // 直接換掉 window 的根控制器,比 push 或 modal 都乾淨
    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
}

- (void)share:(UIButton *)shareButton {
    shareButton.selected = !shareButton.isSelected;
}

分享按鈕的重點在於「選取態」與「普通態」兩張圖的切換,點擊時只做一件事:把 selected 反轉。這裡千萬不要用 self.view.window 去拿主視窗,那是不可靠的寫法,正確做法是透過 UIApplication 取得 keyWindow。

四、頁碼同步與狀態列

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    double doublePage = scrollView.contentOffset.x / scrollView.width;
    int intPage = (int)(doublePage + 0.5); // 四捨五入
    self.pageControl.currentPage = intPage;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

另外提醒一個常被忽略的細節:判斷 4 吋螢幕時,若直接寫 [UIScreen mainScreen].bounds.size.height == 568.0 也可以,但專案裡通常會包成宏 #define FourInch ([UIScreen mainScreen].bounds.size.height == 568.0),讓語意在閱讀時更清楚。

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