您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 開發項目篇 09:圖片命名規則與新版本特性引導頁

編輯:iOS 相關資訊

一、iOS 開發中的圖片加載規則

非 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 是啟動圖,才會自動加載)

舉例(以下皆為系統自動加載)

(1)home 是啟動圖片的情況

iPhone 1 / 3G / 3GS  — 3.5 吋 非 retina :home.png
iPhone 4 / 4S        — 3.5 吋 retina    :home@2x.png
iPhone 5 / 5S / 5C   — 4.0 吋 retina    :home-568h@2x.png

(2)home 不是啟動圖片的情況

iPhone 1 / 3G / 3GS  — 3.5 吋 非 retina :home.png
iPhone 4 / 4S        — 3.5 吋 retina    :home@2x.png
iPhone 5 / 5S / 5C   — 4.0 吋 retina    :home@2x.png

(3)總結

home.png          → 3.5 吋 非 retina
home@2x.png       → retina
home-568h@2x.png  → 4.0 吋 retina + 啟動圖片

這裡最容易被忽略的是:@2x 與 -568h 的自動加載只在「啟動圖」這個特定情境下由系統代勞。一般程式碼中呼叫 [UIImage imageNamed:] 時,4 吋螢幕不會自動去找 -568h 的圖,必須自己動手組出名稱。

二、分享與新版本引導頁的實作

#import "YYNewfeatureViewController.h"
#import "YYTabBarViewController.h"

#define YYNewfeatureImageCount 4

@interface YYNewfeatureViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;
@end

@implementation YYNewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupScrollView];   // 1. 添加 UIScrollView
    [self setupPageControl];  // 2. 添加 pageControl
}

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

    // 每張圖片的寬高與 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@2x 圖片
            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.bounces = NO;                          // 去除彈簧效果
}

#pragma mark - 添加 pageControl
-(void)setupPageControl {
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = YYNewfeatureImageCount;
    pageControl.centerX = self.view.width * 0.5;
    pageControl.centerY = self.view.height - 30;
    [self.view addSubview:pageControl];

    pageControl.currentPageIndicatorTintColor = YYColor(253, 98, 42);   // 當前頁
    pageControl.pageIndicatorTintColor        = YYColor(189, 189, 189); // 其他頁

    self.pageControl = pageControl;
}

#pragma mark - 最後一頁加上兩個按鈕
-(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;
    // 注意:這樣才能同時適配 3.5 吋與 4.0 吋
    startButton.centerY = self.view.height * 0.8;

    [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];

    // 切換控制器的三種方式:
    // [self.navigationController pushViewController:vc animated:NO];   // 目前沒有導航控制器
    // [self presentViewController:vc animated:NO completion:nil];     // modal
    // 注意:不要用 self.view.window 去取得主視窗
    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
}

-(void)setupShareButton:(UIImageView *)imageView {
    UIButton *shareButton = [[UIButton alloc] init];
    [imageView addSubview:shareButton];

    [shareButton setTitle:@"分享給大家" forState:UIControlStateNormal];
    [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_true"]  forState:UIControlStateSelected];
    [shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];

    shareButton.size = CGSizeMake(150, 35);
    shareButton.centerX = self.view.width * 0.5;
    shareButton.centerY = self.view.height * 0.7;

    shareButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);   // 間距
}

-(void)share:(UIButton *)shareButton {
    shareButton.selected = !shareButton.isSelected;   // 狀態取反
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 拿到浮點數後四捨五入
    double doublePage = scrollView.contentOffset.x / scrollView.width;
    int intPage = (int)(doublePage + 0.5);
    self.pageControl.currentPage = intPage;
}

#pragma mark - 隱藏狀態列
-(BOOL)prefersStatusBarHidden {
    return YES;
}

@end

三、程式碼要點說明

(1)手動加載 -568h@2x 圖片

if (FourInch) {   // 需要手動去加載 4 吋螢幕對應的 -568h@2x 圖片
    name = [name stringByAppendingString:@"-568h"];
}

搭配的巨集定義:

// 是否為 4 吋螢幕
#define FourInch ([UIScreen mainScreen].bounds.size.height == 568.0)

(2)UIScrollView 的細節設定

scrollView.contentSize = CGSizeMake(YYNewfeatureImageCount * imageW, 0);  // 活動範圍
scrollView.backgroundColor = YYColor(246, 246, 246);                      // 背景色
scrollView.showsHorizontalScrollIndicator = NO;                           // 隱藏滾動條
scrollView.bounces = NO;                                                  // 去除彈簧效果

contentSize 是橫向滑動的前提,少設它就只會看到第一頁。bounces 設為 NO 則避免使用者把引導頁拖過頭,視覺上比較克制。

(3)同步頁碼

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    double doublePage = scrollView.contentOffset.x / scrollView.width;
    int intPage = (int)(doublePage + 0.5);
    self.pageControl.currentPage = intPage;
}

+ 0.5 再取整數,就是手動的四捨五入。用 contentOffset 除以單頁寬度得到浮點頁碼,是同步 UIPageControl 最直白的做法。

四、幾個值得改進的地方

第一,引導頁顯示完應該把「已看過」寫進 NSUserDefaults,下次啟動直接跳過,這段程式目前每次冷啟動都會重播。

第二,startButton 用 centerY = self.view.height * 0.8 來同時適配兩種螢幕高度,在更長的螢幕上比例會偏移;用 Auto Layout 或按比例約束會更穩健。

第三,直接覆寫 keyWindow.rootViewController 雖然有效,但會讓引導頁與主介面的切換缺乏過場。若在意體感,可用 view controller 的 transitioningDelegate 做一個淡出動畫。

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