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

iOS 螢幕適配實戰:旋轉、解析度與裝置判斷的常用套路

編輯:iOS 開發綜合

適配這件事,說穿了就是在處理「尺寸、比例、裝置能力」三個問題。下面把日常最常翻閱的幾段片段整理成一份備忘錄。

一、處理旋轉

第一步,註冊方向變更通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(changeFrames:)
                                            name:UIDeviceOrientationDidChangeNotification
                                          object:nil];

第二步,在回呼裡依方向重算 frame:

- (void)changeFrames:(NSNotification *)notification {
    NSLog(@"change notification: %@", notification.userInfo);
    float width  = [[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale];
    float height = [[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale];

    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
        || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
        NSLog(@">>>portrait");
        self.frame = CGRectMake(0, 0, height, width);
    } else {
        NSLog(@">>>landscape");
        self.frame = CGRectMake(0, 0, width, height);
    }
    NSLog(@"view—> %@", self);
}

二、取得螢幕解析度

// 目前螢幕尺寸(單位是 point)
CGSize size_screen = [[UIScreen mainScreen] bounds].size;
// 縮放倍率
CGFloat scale_screen = [UIScreen mainScreen].scale;
// 尺寸寬高乘以 scale,就是實際像素(解析度)

// 產生 PDF 時常見的紙張尺寸
CGRect sizeOfA4 = CGRectMake(0, 0, 595, 842);
CGRect sizeOfA5 = CGRectMake(0, 0, 421, 595);

注意:不論 scale 是 1 還是 2,A4、A5 這些紙張尺寸都不用改。因為 iOS 體系裡我們設定的寬高一律是邏輯上的 point,不是真實像素。只要螢幕是等比例縮放,bounds 的值就不變,系統會自行放大或縮小——這也是同一個 App 能在 320×480 與 640×960 上無差異運行的主要原因。

三、常見裝置規格

iPhone / iPod Touch(320 × 480 點)
普屏 320 × 480 像素;Retina 640 × 960 像素。

iPad、iPad 2、New iPad(768 × 1024 點)
普屏 768 × 1024 像素;Retina 1536 × 2048 像素。

換算關係:iPhone 3 上 1 point = 1 pixel,iPhone 4 起 1 point = 2 pixel。對應到圖片命名,普屏用 image.png,Retina 屏用 image@2x.png。

四、模擬器判斷與裝置類型判斷

#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
NSLog(@"on simulator");
#else
NSLog(@"not on simulator");
#endif

注意:TARGET_OS_IPHONE 在真機與模擬器上都會是 1,不能拿來分辨環境。

裝置類型有兩種判斷方式。第一種用 UI_USER_INTERFACE_IDIOM()(iOS 3.2 以上),缺點是分不出 iPhone 與 iPod:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // iPad
} else {
    // iPhone 或 iPod
}

第二種用 UIDevice.model(iOS 2.0 以上),可以細分機型:

NSString *deviceType = [UIDevice currentDevice].model;
if ([deviceType isEqualToString:@"iPhone"]) {
    // iPhone
} else if ([deviceType isEqualToString:@"iPod touch"]) {
    // iPod Touch
} else {
    // iPad
}

五、讀取裝置與 App 資訊

// 系統資訊
NSLog(@"sysname=%@", [[UIDevice currentDevice] systemName]);        // 系統名稱
NSLog(@"systemVersion=%@", [[UIDevice currentDevice] systemVersion]); // 版本號
NSLog(@"model=%@", [[UIDevice currentDevice] model]);               // 機型
NSLog(@"name=%@", [[UIDevice currentDevice] name]);                 // 裝置名稱
NSLog(@"localizedModel=%@", [[UIDevice currentDevice] localizedModel]);
// iOS 6 起可用;uniqueIdentifier 從 iOS 5 起已標記為 deprecated
NSLog(@"ios6UUID=%@", [[[UIDevice currentDevice] identifierForVendor] UUIDString]);

// App 資訊
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Name    = [infoDictionary objectForKey:@"CFBundleDisplayName"];
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *app_build   = [infoDictionary objectForKey:@"CFBundleVersion"];

// 是否有相機
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    NSLog(@"有");
} else {
    NSLog(@"沒有");
}

六、面對不同解析度,開發者只需做三件事

1. 準備 App 的高解析圖示。
2. UI 素材提供 @2x.png。
3. 從網路下載圖片時做條件判斷:先確認 respondsToSelector:@selector(scale),再看 scale 是否等於 2。

由於所有 iPhone、iPod Touch 的邏輯解析度都是 320×480,App 不改也能跑,只是原本的圖會被拉伸,白白浪費 Retina 的優勢。程式裡所有 CGRectMake 用的都是邏輯解析度,不會影響位置與大小——這對遊戲開發特別有用,一般 App 影響有限。

最後是相對佈局的問題:iOS 6 之前只能靠 autoresizingMask,6.0 之後則可以用接近 Android 相對佈局思路的 Auto Layout。

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